vc调用matlab的m文件混合编程(引擎方式)

/*********************************引擎调用*******************************/;bool readFile(vector<double> &vec_in, ifstream &infile){double temp;while(infile>>temp)vec_in.push_back(temp);return true;}bool ellipsefit_engine(Engine *ep,double &Xc,double &Yc,double &A,double &B,double &Phi,double &P,vector<double> x,vector<double> y){mxArray *mxa_x, *mxa_y;mxArray *mxa_Xc = NULL,*mxa_Yc = NULL,*mxa_A = NULL,*mxa_B = NULL,*mxa_Phi = NULL,*mxa_P = NULL;mxa_x = mxCreateDoubleMatrix(x.size(),1 , mxREAL);mxa_y = mxCreateDoubleMatrix(y.size(),1 , mxREAL);memcpy(mxGetPr(mxa_x),&x[0], y.size()*sizeof(double));engPutVariable(ep,”x”,mxa_x);engPutVariable(ep,”y”,mxa_y);mxDestroyArray(mxa_x);mxDestroyArray(mxa_y);engEvalString(ep,”userpath(‘C:\Users\Administrator\Desktop\eclipse_c\test’);”);//更改matlab工作空间,,使其指向调用的m文件的地址engEvalString(ep,”[Xc,Yc,A,B,Phi,P]=ellipsefit(x,y);”);//前边一定要更改空间,使其指向函数[Xc,Yc,A,B,Phi,P]=ellipsefit(x,y)所在的m文件mxa_Xc=engGetVariable(ep,”Xc”);mxa_Yc=engGetVariable(ep,”Yc”);mxa_A=engGetVariable(ep,”A”);mxa_B=engGetVariable(ep,”B”);mxa_Phi=engGetVariable(ep,”Phi”);mxa_P=engGetVariable(ep,”P”);double *p_Xc = mxGetPr(mxa_Xc);// 将 matlab 中的矩阵的指针传递给 C 语言中的指向 double 的指针Xc=p_Xc[0];double *p_Yc =mxGetPr(mxa_Yc);Yc = p_Yc[0];double *p_A =mxGetPr(mxa_A);A = p_A[0];double *p_B =mxGetPr(mxa_B);B = p_B[0];double *p_Phi =mxGetPr(mxa_Phi);Phi = p_Phi[0];double *p_P =mxGetPr(mxa_P);P = p_P[0];return true;}int main(int argc, _TCHAR* argv[]){Engine* pEng=NULL;if(!(pEng=engOpen(NULL))){printf(“Open matlab engine fail!\n”);return 0;}elseprintf(“Open Engine Sucess!\n”);engSetVisible(pEng,0);//隐藏matlab窗口,1显示窗口cout << “开始运行” << endl;ifstream infile_x,infile_y;char *infile_name_x=”x.txt”;char *infile_name_y=”y.txt”;infile_x.open(infile_name_x);vector<double> vec_x;readFile(vec_x,infile_x);infile_y.open(infile_name_y);vector<double> vec_y;readFile(vec_y,infile_y);double Xc,Yc,A,B,Phi,P;ellipsefit_engine(pEng,Xc,Yc,A,B,Phi,P,vec_x,vec_y);cout<<“Xc=”<<Xc<<endl;cout<<“Yc=”<<Yc<<endl;cout<<“A=”<<A<<endl;cout<<“B=”<<B<<endl;cout<<“Phi=”<<Phi<<endl;cout<<“P=”<<P<<endl;printf(“Engine will be closed\n”);system(“pause”);if(NULL!=pEng)engClose(pEng);printf(“Engine is closed”);return 0;}

穷则思变,差则思勤!没有比人更高的山没有比脚更长的路。

vc调用matlab的m文件混合编程(引擎方式)

相关文章:

你感兴趣的文章:

标签云: