logo资料库

VS2015与MATLAB联合调用.doc

第1页 / 共2页
第2页 / 共2页
资料共2页,全文预览结束
VS2015 中 C++与 MATLAB 联合调用-引擎方式 1、设置好 VS 中的 include 路径 项目,属性,配置属性,VC++目录,包含目录, 增加 C:\Program Files\MATLAB\R2017b\extern\include 2、设置好 VS 中的 lib 路径 项目,属性,配置属性,VC++目录,库目录, 增加 C:\Program Files\MATLAB\R2017b\extern\lib\win64\microsoft 3、设置系统变量 Path 中增加 C:\Program Files\MATLAB\R2017b\bin\win64 4、代码中增加头文件和预编译库。 #include"engine.h" #pragma comment(lib,"libeng.lib") #pragma comment(lib,"libmx.lib") #pragma comment(lib,"libmat.lib") 5、部分代码 Engine* pEng = NULL; if (!(pEng = engOpen(NULL)))
AfxMessageBox(L"Open matlab engine fail!"); { } else AfxMessageBox(L"Open Engine Sucess!"); engSetVisible(pEng, 0);//隐藏matlab窗口 mxArray *A = NULL; double init = 2; A = mxCreateDoubleMatrix(1, 1, mxREAL); memcpy((void*)mxGetPr(A), (void*)&init, sizeof(double)); engPutVariable(pEng, "A", A); init = 1; memcpy((void*)mxGetPr(A), (void*)&init, sizeof(double)); engPutVariable(pEng, "B", A); mxDestroyArray(A); engEvalString(pEng, "t=0:0.2:7;plot(t,sin(t));"); engEvalString(pEng, "figure;pause(0.2)"); engEvalString(pEng, "plot(t,A.*cos(t)+B);"); engEvalString(pEng, "C=A+B"); AfxMessageBox(L"Pause!"); mxArray *C = NULL; C = mxCreateDoubleMatrix(1, 1, mxREAL); C = engGetVariable(pEng, "C"); double data; memcpy(&data, (void*)mxGetPr(C), sizeof(data)); CString msg; msg.Format(L"C=%0.2f", data); AfxMessageBox(msg); if (NULL != pEng) engClose(pEng); AfxMessageBox(L"Engine is closed");
分享到:
收藏