原文地址:https://blog.youkuaiyun.com/jkhere/article/details/8906274#commentsedit
#include "mex.h"
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
int i;
i=mxGetScalar(prhs[0]); //get input parameter
if(i==1)
mexPrintf("hello,world!\n");
else
mexPrintf("大家好!!!!\n");
}
上面是一个简单的C++代码,保存成helloworld.cpp。简单解释一下:
- void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
是MEX的接口子程序,接口子程序是与MATLAB的接口,实现两个不同内存空间中的通信。
在matlab命令行中:
输入 mex helloworld.cpp,编译成功。
结果:
>> mex hello.c
使用 'MinGW64 Compiler (C)' 编译。
MEX 已成功完成。
>> hello(1)//必须传参,不传参matlab报错
hello,world!
>> hello(0)
大家好!!!!
>>