VC and MATLAB mixed programming call dynamic link library dll--< two >
1, is the first to establish the M file matlab.
Ellipsefit.m
It contains: function [Xc,yc,a,b,phi,p]=ellipsefit (x, y)
2.MCC command compilation generated by H, DLL, lib and other files
In the MATLAB command line, enter:
mcc-w cpplib:ellipsefit ellipsefit.m-t link:lib (this mode is C + + style function)
(The specific command meaning can be referred to "proficient in MATLAB and C + + mixed Programming" Liouville, 3rd edition, Beijing University of Aeronautics and Astronautics Press. )
(Ellipsefit is the generated LIB file name, which can be arbitrarily named.) ELLIPSEFIT.M is the. m file name that you want to invoke.
At this point, you can see that a series of files are generated in the directory where ELLIPSEFIT.M is located. We will use:ellipsefit.h, Ellipsefit.dll, Ellipsefit.lib
2.3: Create a VC project: Test
For simplicity, we are building a Win32 console program here. As follows:
Then, put the above 3 files (ellipsefit.h, Ellipsefit.dll, Ellipsefit.lib
) to the directory in which the VC works, so that the VC can call it and add the. h file to the project.
Among them, the ellipsefit.h中的function [Xc,Yc,A,B,Phi,P]=ellipsefit(x,y)
corresponding C + + style of the function interface is:
externvoid MW_CALL_CONV ellipsefit(intconstconst mwArray& y);
That is, call the interface in your own main program, using the Mwarray matrix for data manipulation in C + +
2.3 VC Programming: (Code below)
//Test.cpp: Defines the entry point of the console application. //#include "stdafx.h"#include <vector>#include <iostream>#include <fstream>/********************************VC tune ì?? Use?? MCC-produced ¨2 |¨2 ì?dll************************************/#include "mclmcrrt.h"#include "mclmcr.h"#include "mclcppclass.h"#include "matrix.h"//Include Lib head file#include "ellipsefit.h"//link matlab sys lib#pragma comment (lib, "Mclmcrrt.lib")#pragma comment (lib, "Libmx.lib")#pragma comment (lib, "Libmat.lib")#pragma comment (lib, "Mclmcr.lib")//Link lib#pragma comment (lib, "Ellipsefit.lib")using namespace STD;BOOLReadFile ( vector<double>&vec_in, Ifstream &infile) {DoubleTemp while(infile>>temp) Vec_in.push_back (temp);return true;}BOOLEllipsefit_dll (Double&XC,Double&YC,Double&a,Double&b,Double&phi,Double&p, vector<double>X vector<double>Y) {Mwarray mwa_x (x.size (),1, Mxdouble_class), Mwa_y (Y.size (),1, Mxdouble_class); Mwa_x.setdata (&x[0],x.size ()); Mwa_y.setdata (&y[0],y.size ()); Mwarray MWA_XC (1,1, Mxdouble_class), MWA_YC (1,1, Mxdouble_class), Mwa_a (1,1, Mxdouble_class), Mwa_b (1,1, Mxdouble_class), Mwa_phi (1,1, Mxdouble_class), Mwa_p (1,1, Mxdouble_class); Ellipsefit (6, mwa_xc,mwa_yc,mwa_a,mwa_b,mwa_phi,mwa_p,mwa_x, mwa_y); XC=MWA_XC (1,1); YC=MWA_YC (1,1); A=mwa_a (1,1); B=mwa_b (1,1); Phi=mwa_phi (1,1); P=mwa_p (1,1);return true;}intMainintARGC, _tchar* argv[]) {if(!mclinitializeapplication (NULL,0)) {cout<<"Could not initialize the application."<< Endl;; }if(!ellipsefitinitialize ()) {cout<<"Can ' t initalize!"<< Endl; }cout<<"Start a" O "D" .<< Endl; Ifstream infile_x,infile_y;Char*infile_name_x="X.txt";Char*infile_name_y="Y.txt"; Infile_x.open (infile_name_x); vector<double>vec_x; ReadFile (vec_x,infile_x); Infile_y.open (infile_name_y); vector<double>vec_y; ReadFile (vec_y,infile_y);DoubleXc,yc,a,b,phi,p; Ellipsefit_dll (xc,yc,a,b,phi,p,vec_x,vec_y);cout<<"xc="<<Xc<<endl;cout<<"yc="<<Yc<<endl;cout<<"A="<<A<<endl;cout<<"b="<<B<<endl;cout<<"phi="<<Phi<<endl;cout<<"p="<<P<<endl; Ellipsefitterminate (); Mclterminateapplication (); System"Pause");return 0;}
VC and MATLAB mixed programming call dynamic link library DLL (c + +)--< two >