Matlab与C++混合编程,C++代码中的函数跟Matlab中的函数一样,都可以使用,hello.cpp中的C++代码为:
#include "mex.h"
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
mexPrintf("Hello world!\n");
}
其中
#include "mex.h"是必须写的,
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])也是必须写的,格式必须这样,然后在Matlab中用mex函数配置好编译环境后,输入mex hello.cpp命令,会形成一个hello.mexw32或hello.mexw64文件,w32是32位windows系统,w64是64位windows系统,linux系统下是.mexa32或
.mexa64,然后在Matlab窗口中输入hello即可调用hello函数,Matlab窗口中输出为Hello world!
上述代码已经过测试,如图