VC Call Matlab7 Description
First, MATLAB7 installation configuration
1. Install (slightly), but need to ensure that the computer on the MATLAB and vs2008
2. Run Mex–setup (select or change the compilation configuration, do not configure the DLL may not be compiled or compiled DLL can not be called by VC)
1) system hint: Would you like MEX to locate installed compilers [y]/n?
Select Y return
2 The results may appear as follows:
Select a compiler:
[1] lcc-win32 C 2.4.1 in F:\TOOLS\DEVTOOLS\MATLAB\R2009B\SYS\LCC
[0] None
If you need to select the VC compiler (in general) need to select N, select n after the results are as follows:
Select a compiler:
[1] Intel C + + 9.1 (with Microsoft Visual C + + SP1 linker)
[2] Intel Visual Fortran 10.1 (with Microsoft Visual C + + Sp1linker)
[3] Microsoft Visual C + + SP1
[4] Microsoft Visual C + + 2008 Express
[5] Microsoft Visual C + + 2008 SP1
Select 5
3 The system may be prompted as follows:
The default location for Microsoft visualc++ 2008 SP1 compilers is C:\Program Files (x86) \microsoft Visual Studio 9.0,
But that directory does not exist on Thismachine.
Use C:\Program Files (x86) \microsoft Visual Studio 9.0 anyway [y]/n?
When the path is correct, you can select Y directly, select n when the path is wrong, and reset the vc2008 installation path
4 after the successful installation appears may prompt:
Use C:\Program Files (x86) \microsoft Visual Studio 9.0 anyway [y]/n?n
Please enter the location of your compiler: [C:\Program Files (x86) \microsoft Visual Studio 9.0] D:\Program Files (x86) \mic Rosoft Visual Studio 9.0
Please verify your choices:
Compiler:microsoft Visual C + + 2008 SP1
Location:d:\program Files (x86) \microsoft Visual Studio 9.0
3. Run Mbuild–setup (configuration basic ditto)
Second, the VC configuration
If the configuration is all right, the include file directory and the library file directory will automatically set up, if there is no automatic setting, you need to manually set up.
1. Set the Include file
Set the include file path in the VC + + directory, add the actual MATLAB include path to the bottom
2. Set the library file directory
Third, MATLAB generation DLL
1. Use Matlab to generate a. m file
2. Call the function in the MATLAB environment
Now, the result is 5.
3. Run mcc-t-h-l c-w lib:ppp-t link:lib myfunct.m
Matlab will prompt-t-h-l These three parameters are no longer used, regardless of it, the working directory will generate a heap of files, of which ppp.h Ppp.lib Ppp.dll three files is what we need. This process may last for a while, as long as you do not complain, you wait a few minutes, up to a minute, in the directory will generate the required files.
can also be used directly
Mcc-wlib:gpsanarmxl-t Link:lib GPSANA.M
Such a command line to compile the build DLL
Where PPP can be specified as a different name
Iv. VC Call DLL
1. Use VC to create a console program
2. Enter the following code in the main function
int _tmain (int argc, _tchar* argv[]) { Initializing the Environment bool bl = Pppinitialize (); Parameter definition Double A[1] = {1.0}; Double B[1] = {2.0}; Double ret; Because the parameters required by Matlab are matrices, it should be defined as follows Mxarray *g = Mxcreatedoublematrix (1, 1, mxreal); Input matrix, X Mxarray *y = Mxcreatedoublematrix (1, 1, mxreal); Call the MATLAB function, the parameters must be matrix form Mxarray *r = Mxcreatedoublematrix (1, 1, mxreal); Call the MATLAB function, the parameters must be matrix form Copy the data to the variables defined in MATLAB memcpy (void *) MXGETPR (G), (void *) A, sizeof (a)); Copy data memcpy (void *) MXGETPR (y), (void *) b, sizeof (b)); Copy data Call function Mlfmyfunct (1, &r, G, y); Get function return value from MATLAB return value Double * MD = MXGETPR (R); Get the data in Mxarray Double result = md[0]; The first element of the array is the value we want. Print return value printf ("%f", result); The MATLAB application variable is released Mxdestroyarray (G); Free memory Mxdestroyarray (y); Pppterminate (); End Call char c; scanf ("%c", &c); return 0; } |
Note: The lib file needs to be added to the project.
3. After the compilation is run, the result is output 3. If we change the parameter to 2, 3, we will output 5, which is the same as the output in MATLAB.
The results are as follows: