Visual Studio and MATLAB are mixed programming in two ways:
1 matlab call C program;
2 vs Call Matlab (currently seeing VS, other compilers such as codeblocks, or not providing this feature);
The first one is to call vs or other compilers with the MEX command of MATLAB and compile the C file in a certain way into a. Mexw32 (for Win32) or. MEXW64 (for Win64), and Matlab can invoke the C program in a way that calls the dynamic-link library. In this process, you only need to install the compiler in the premise that the execution:
Mex-setup
And
Mbuild-setup
and follow the prompts step by step to help Matlab to find C compiler on it.
The other is, in turn, called Matlab by VS, because in the final analysis, many of the underlying code relies on the C/s + + language, if you want to use MATLAB for real-time data processing, it is necessary to use C/s (from the operating system kernel, sockets, or devices) to obtain data, Then call MATLAB for processing. Cut to the chase:
1 First install the environment correctly, the following is the environment I use:
x86
Windows 7
VS2013
MATLAB r2010a (Installation directory is D:\MATLAB\R2010a).
2 Below is the inclusion of the VS path :
Right-click Project-Properties-vc++ Directory
-Include directories, add D:\MATLAB\R2010a\extern\include; (note semicolon)
-Library directory, add D:\MATLAB\R2010a\extern\lib\win32\microsoft;
-c/c++ (-General), add additional include directory D:\MATLAB\R2010a\extern\include\;
-Linker
-General, add additional library directory D:\MATLAB\R2010a\extern\lib\win32\microsoft;
-Input, add additional dependencies: Libeng.lib;libmat.lib;libmex.lib;libmx.lib;mclmcrrt.lib;mclmcr.lib;
3 System environment variable : add D:\MatLab\R2010a\bin\win32 in Path; Help vs find the provided dynamic link library for MATLAB.
4 registering the MatLab server : cmd, running in the D:\MatLab\R2010a\bin directory
Matlab \regserver
Probably open the MATLAB server mode and receive the VS call request.
5 The following write-C + + programs:
To add a header file and a dynamic-link library :
" engine.h " #pragma comment (lib, "Ws2_32.lib")#pragma comment (lib, "Libeng.lib")#pragma comment (lib, "Libmx.lib")#pragma comment (lib, "Libmat.lib")
Open the engine (used by the engine to invoke MATLAB):
engine* pEng = NULL; if (! (PEng = Engopen (NULL))) { printf ("Open matlab enging fail! " ); GetChar (); return -1; }
engine Open function Openeng after the call is finished, be sure to check whether the Open is successful (similar to the cloud malloc function, the memory must be checked for success), because even if peng==null, In the function engevalstring execution also does not throw the exception, will only cause the enggetvariable execution to fail, of course, because did not carry out, where comes the variable?
Then it can be called, all of the MATLAB commands can be passed through the following function into the MATLAB execution, similar to the Linux system called the EXEC ("...") and the Windows systems ("..."):
" x=0:0.01:pi); Y=sin (x); Plot (x, y)");
If you want to get back results from MATLAB:
engevalstring (pEng, x=0:0.02:3.14; Y=sin (x); Plot (x, y) *x= Enggetvariable (PEng, " x " "; Mxarray *y= enggetvariable (pEng, y " double * _x = MXGETPR (x); double * _x = Mxgetpr (y); // _x is an array of x at this time, _y is the y array
Since Matlab, the data will be treated as a matrix, so the return result is also returned as an array, if it is a single value, is array[0], otherwise array[0..n-1], how to get n? or using the function enggetvariable, you can execute N=length (x) First, then return n .
when you are done, remember to close the engine :
if (pEng) engclose (pEng);
Note that once you close, you can no longer use Peng, or you will throw an exception. also, when the engine is shut down, the figures that was originally obtained during execution are closed. In my project, the engine was opened at the beginning of the program and closed when the program finally exited.
6 if the "XXX.lib not found" condition occurs during compilation, check that the path is added correctly, or copy the Lib file directly to System32 ...
7 if the compile link through, run up or there is an exception, check whether MATLAB server has registered success: Run Matlab/regserver, and then try again.
8 because the command invoked by MATLAB is passed in as a string, be sure to note the special symbols and spaces inside the string . Special symbols need to precede the backslash, if there are spaces, according to the syntax of MATLAB, you should use single quotation marks . For example:
Engevalstring (PEng,"");
Above ~
VS2013 calling Matlab mixed programming