需要提前安装visual studio enterprise 2017等c/c++编译器
测试过程发现MEX 编译FORTRAN程序总是失败,C成功了
然后 >>mex -setup -V
选择c++编译器。最后可以写个c语言的函数int add(int,int);加上一个mexfunction
#include<mex.h>
int wei(int a,int b)
{
int t;
t=a+b;
return t;
}
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
double a,b;//prhs专门用来管理前面函数的输入参数
extern int wei(int,int);
a = mxGetScalar(prhs[0]);//输入a是局部参数
//从设备指针得到scar,y=f(x)
b = mxGetScalar(prhs[1]);
//管理等式右端第二个参数,就是参数,p是pointer
plhs[0]=mxCreateDoubleMatrix(1,1,mxREAL);
//只是创建了一个数据层的指针,普通管理员
double *c;
c=mxGetPr(plhs[0]);//返回c必须是指针,要存在内存中。
*c=wei(a,b);
}