mex -setupmbuild -setup
都选则VS2010的对应选项。
执行:mcc -W lib:addtest -T link:lib e:\temp\myadd.m -d e:\temp
后可以看到,在e:\temp中生成了若干以addtest命名的文件,分别有.dll .lib .h等
四、在VS中调用
int test_matlab_myadd()
{
addtestInitialize();
static double x1[4] = {1.0, 2.0, 100.5, 200.1};
static double x2[4] = {2.5, 1.3, 200.1, 100.5};
double result[4];
mxArray * A = mxCreateDoubleMatrix(1, 4, mxREAL);
memcpy (mxGetPr(A), &x1, sizeof(double) * 4);
mxArray * B = mxCreateDoubleMatrix(1, 4, mxREAL);
memcpy (mxGetPr(B), &x2, sizeof(double) * 4);
mxArray * C = mxCreateDoubleMatrix(1, 4, mxREAL);
mlfMyadd (1, &C, A, B);
memcpy (&result, mxGetPr(C), sizeof(double) * 4);
std::cout << result[0] <
mxDestroyArray(A);
mxDestroyArray(B);
mxDestroyArray(C);
addtestTerminate();
return 0;
}
五、总结
利用类似的方法也可以导出MATLAB中自带的函数。比如norminv函数:在MATLAB中执行: mcc -W lib:norminv -T link:lib norminv.m -d e:\temp在VC中编码
int test_matlab_norminv()
{
norminvInitialize();
static double P = 0.9;
mxArray * mxa_P = mxCreateDoubleMatrix(1, 1, mxREAL);
memcpy (mxGetPr(mxa_P), &P, sizeof(double));
static double mu = 0;
mxArray * mxa_mu = mxCreateDoubleMatrix(1, 1, mxREAL);
memcpy (mxGetPr(mxa_mu), &mu, sizeof(double));
static double sigma = 1.0;
mxArray * mxa_sigma = mxCreateDoubleMatrix(1, 1, mxREAL);
memcpy (mxGetPr(mxa_sigma), &sigma, sizeof(double));
mxArray * mxa_pcov = mxCreateDoubleMatrix(1, 1, mxREAL);
memcpy (mxGetPr(mxa_pcov), &sigma, sizeof(double));
mxArray * mxa_alpha = mxCreateDoubleMatrix(1, 1, mxREAL);
memcpy (mxGetPr(mxa_alpha), &sigma, sizeof(double));
mxArray * mxa_X = mxCreateDoubleMatrix(1, 1, mxREAL);
/*mxArray * mxa_XLO = mxCreateDoubleMatrix(1, 1, mxREAL);
mxArray * mxa_XUP = mxCreateDoubleMatrix(1, 1, mxREAL);*/
//bool bRes = mlfNorminv (1, &mxa_X, &mxa_XLO, &mxa_XUP, mxa_P, mxa_mu, mxa_sigma, mxa_sigma, mxa_sigma);
bool bRes = mlfNorminv (1, &mxa_X, NULL, NULL, mxa_P, mxa_mu, mxa_sigma, mxa_sigma, mxa_sigma);
double res;
memcpy (&res, mxGetPr(mxa_X), sizeof(double));
mxDestroyArray(mxa_P);
mxDestroyArray(mxa_mu);
mxDestroyArray(mxa_sigma);
norminvTerminate();
return 0;
}