Csunking contribution, 2015-9-22 1712
1. Overview
By using C/C + + and MATLAB mixed programming, you can enjoy the fast execution speed of the code, but also easy to use MATLAB Many library functions and powerful drawing capabilities. Let Matlab call C function is implemented through DLL file, and this DLL development process can not only use the MEX command with MATLAB, also can use VC + + development environment, use VC has many advantages, one is to let me back to the original familiar development environment, two can use more standard C + + compiler, the 3rd is also the most cool, you can use the VC powerful debugging functions.
The development environment used for this document update is: Visual Studio, Matlab r2015a
2. Steps
1. Start VS, create a new empty project Atsrid. Then configure the Project property information. Then, create a new. cpp file atsrid.cpp. and add a Matlab_mex_file macro definition in the Add preprocessor definitions.
Change the suffix name of the output file to Mexw64/mexw32
$ (OutDir) $ (TargetName). mexw64
Configure the platform number properties for the Project Builder
2. Provide support for the header file and the static library for MATLAB functions
Include directory:
E:\Program Files\matlab\r2015a\extern\include
E:\Program Files\matlab\r2015a\extern\include\win64
Library Directory
E:\Program Files\matlab\r2015a\extern\lib\win64\microsoft
Add a static link library file to the connector
Libmx.lib;libmat.lib;libmex.lib;
3. Write the contents of the Mex file in Atsrid.cpp. Then use the. def file to export the Mexfunction function.
#include "mex.h" void mexfunction (int nlhs,mxarray *plhs[],int nrhs,const mxarray *prhs[]) { int nlhs1,nrhs1; Mxarray *plhs1[1],*prhs1[1]; Mxarray *array_ptr; nlhs1=1; nrhs1=1; if ((Array_ptr=mexgetvariable ("base", "X") ==null) //"base" means the command window where { //we get the Matrix "x". Mexerrmsgtxt ("Variable ' X ' isn't in the workspace."); else { prhs1[0]=array_ptr; Mexcallmatlab (NLHS1,PLHS1,NRHS1,PRHS1, "Det"); mexprintf ("Det (array_ptr) = =%g\n", Mxgetscalar (Plhs1[0]));} }
There are two ways to export a function from a DLL, __declspec (dllexport) command export and DEF file export, but since the need to export mexfunction is defined in "Mex.h", the previous export method does not work, here the Def file is exported.
First tell the compiler that I used the Xx.def file, and then add the content to the file. def files defined in this way are automatically added to the following properties.
The contents of the file are as follows
LIBRARY Atsrid; Refers to the module name (you can use the new project name)
Exports mexfunction
4. Compile and run, generate the Atsrid.mexw64 file. The file is then copied to the current directory in MATLAB, and then the module is tested
X=magic (100);
Atsrid ();
You can compare the results with DET (x) to see if they are correct.
Note: The Atsrid.cpp in the VS project can be placed directly in the current directory of MATLAB for Mex Atsrid.cpp compilation, and can also generate ATSRID.MEXW64. The reason for compiling in Visual C + + is because it is convenient to debug the program. Another debugging method is described below.
3. Using the VS Debug Mex file
Convenient to you in MATLAB on the researcher or developer.
1. Configure the configuration Matlabmex compiler for the first time you use MEX
Mex-setup
2, Next, with the-G option to compile the Mex source file,-G indicates that the compilation results contain debugging information.
Mex-g Atsrid.cpp
3. Open visual Studio and do not close your MATLAB. In Visual Studio, on the Tools menu, select Associate to a process.
Visual Studio Loads the data, selects it in the popup dialog box, selects the MATLAB process, and clicks "Associate".
4. First add a breakpoint at the Atsrid.cpp file code in the Atsrid project in Visual Studio. Then, open matlab to place the current directory of ATSRID.MEXW64, and enter the following code in the line of code
X=magic (10);
Atsrid ();
Later, you will find that Visual Studio will automatically run the program to the breakpoint and wait for the next action.
When the breakpoint is triggered, you can use Visual Studio to view your variables, memory, and debug your program.
Reference
Homecoming,http://knhunter.blogbus.com/.
Developing a Matlab Mex file in Visual Studio, generating Mexw64/mexw32