注意事项:
1.
2.
3.
mxArray
double time[10]={0,1,2,3,4,5,6,7,8,9};
mxArray *sun=NULL;
double *num_err;char suncmd[200]="a=1;b=30;c=a+b;";
//方法一为对每个matlab的运行语句进行逐个书写运行
T=mxCreateDoubleMatrix(1,10,mxREAL);
memcpy((char*)mxGetPr(T),(char*)time,10*sizeof(double));
engPutVariable(ep,"T",T);
engEvalString(ep,"D=.5*(-9.8).*T.^2;");
engEvalString(ep,"plot(T,D);");
engEvalString(ep,"title('position vs. time for a falling object');");
engEvalString(ep,"xlabel('tiem(seconds)');");
engEvalString(ep,"ylabel('position(meters)');");
方法二:把需要运行的程序放在一个字符数组中,一句话进行调用,最后只提取自己感兴趣的变量。
engEvalString(ep,suncmd);
if((sun=engGetVariable(ep,"c"))==NULL)
{
op_sim_end("'t get the variable","","","");
}
else
{
num_err = mxGetPr(sun);
}
printf("the number is: %d",(int) *num_err);
方法三:把书写好的matlab的m文件写好后存在matlab的搜索路径里面。在c语言中只是对这个函数名进行直接调用就可以了。
engEvalString(ep,"abc");
if((sun=engGetVariable(ep,"c"))==NULL)
{
op_sim_end("'t get the variable","","","");
}
else
{
num_err = mxGetPr(sun);
}
printf("the number is: %d",(int) *num_err);
// 释放使用完的空间
mxDestroyArray(T);
mxDestroyArray(sun);
//方法一为对每个matlab的运行语句进行逐个书写运行
T=mxCreateDoubleMatrix(1,10,mxREAL);
//