The realization method of matlab and c mixed programming

System: win7 64bit
matlab version: 9.1.0.441655 (R2016b)


1) Baidu download and install TDM-GCC MinGw Compiler, record its installation path during the installation process (do not modify the automatically generated path)
2) After installation, right-click "Computer" -> "Properties" -> "Advanced System Variables" -> "Environment" Variables"->"System Variables"->"New":

Variable name: MW_MINGW64_LOC
Variable value: installation path

3) Enter in matlab:

>>setenvc('MW_MINGW64_LOC','installation path')

4) restart matlab
5) enter

>>mex_setup

  设置操作到这里基本就可以告一段落了,接下来我们要对c++源文件做一些改变才能够使matlab引用和执行。这里举个例子比较容易明白过程。还有些地方没有搞太懂,留在以后真正用到的时候在做补充。

1) Suppose you have written a c++ source file, such as 123.cpp
2) Perform the syntax transformation of the mixed programming of c++ and matlab on the source file
3) Input in matlab

>>mex 123.cpp

After that, a 123.mexw64 file is generated
4) Input in matlab

>>123()

call the function


Modifications to 123.cpp:
1) Add header files

#include”mex.h”

2) Interface interface function

void mexFunction (int nlhs, mxArray * plhs [], int nrhs, const mxArray * prhs []) {}

Explanation of parameters:

nlhs: Indicates that matlab's calling command has several variables to the left of the equal sign.
plhs: Pointer to the variable on the left side of the equal sign in the call command of matlab.
nrls: Indicates the number of variables on the right side of the equal sign in the call command of matlab.
prhs: Instructs matlab to call the variable pointer to the right of the equal sign.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325615535&siteId=291194637