Matlab编程
要在visual studio2008调用matlab2009b,目前装在win7 x64系统。
(1)基本配置
a. use vs install file to modify and add feature, vc++, check the x64 component, and install it
b. add include files
D:\Program Files\MATLAB\R2009b\extern\include
D:\Program Files\MATLAB\R2009b\extern\include\win64
add lib files
D:\Program Files\MATLAB\R2009b\extern\lib\win64\microsoft
libeng.lib libmx.lib
c. build->configuration manager->platform-add new x64
d. run, and you may encounter a problem, "libeng.dll is not lost".
add an system environment path(MyCopmputer->Properties->Advanced->SystemPath):
D:\Program Files\MATLAB\R2009b\bin\win64
e. finally it's ok. test the examples, such as:
D:\Program Files\MATLAB\R2009b\extern\examples\eng_mat\engdemo.cpp
(2)解决vs2008与x64matlab库兼容问题
matlab安装的为x64的库,vs2008默认为x86编译项目,没有x64,但是可以添加组件,编译x64平台应用。因此,需要协调版本问题,本想将matlab重装成x86发现没有这个选项。配置vs2008解决版本问题:
在build->configuration manager->add x64 platform,重新编译即可。
由于在能用qt,重新编译报错,没有可以用的版本。直接项目-右键qt属性,设置使用中qt版本,刚刚设置的x64编译平台,自动变回win32.
查找n久后,解决方案:打开*..vcproj,将QtVersion所在的<>删除,重新工程右键qt project properties, 选择使用中的库。
(3)使用在vs2008调用matlab的fsolve求解非线性最小二乘问题
如何传入和传出数组?
如何调用matlab自带函数?
如何能使用自己写的.m,因为fsolve的的参数有一个自己写的.m
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "engine.h"
#include <string>
#define BUFSIZE 256
#define LOCAL_MATLAB_DIR "cd path // where the .m file is stored"
using namespace std;
int main()
{
Engine *ep;
mxArray *T = NULL, *result = NULL;
double R[1] = { 2 };
if (!(ep = engOpen("/0"))) {
fprintf(stderr, "\nCan't start MATLAB engine\n");
return EXIT_FAILURE;
}
T = mxCreateDoubleMatrix(1, 1, mxREAL);
memcpy((void *)mxGetPr(T), (void *)R, sizeof(R));
engPutVariable(ep, "T", T);
engEvalString(ep, LOCAL_MATLAB_DIR);
if(engEvalString(ep, "D=fsolve(@myfun,T);") == 0)
printf("engEvalString encounters a problem.");
printf("\nRetrieving D...\n");
if ((result = engGetVariable(ep,"D")) == NULL)
printf("Oops! You didn't create a variable F.\n\n");
else {
printf("F is class %s\t\n", mxGetClassName(result));
}
double *newR;
newR = mxGetPr(result);
printf("%f %f", newR[0], newR[1]);
printf("Hit return to continue\n\n");
fgetc(stdin);
printf("Done!\n");
mxDestroyArray(T);
mxDestroyArray(result);
engClose(ep);
return EXIT_SUCCESS;
}
解决问题参考资料:
http://newsgroups.derkeiler.com/Archive/Comp/comp.soft-sys.matlab/2010-03/msg00631.html
http://www.mathworks.com/support/solutions/en/data/1-2CEUK6/index.html?solution=1-2CEUK6
How can I debug MATLAB code in my MATLAB Engine application?
Problem Description:
I realize I cannot use the debugger to debug MATLAB code while calling it from a MATLAB Engine application.
However, I need to verify that my variables are being passed in correctly. If they are, I then need to debug my code to see why my answers are not what I expect.
Solution:This change has been incorporated into the documentation in Release 2011a (R2011a). For previous releases, read below for any additional information:
While examining the results of myCmxArrayResult, you might find that the result is not what you expect. Since you saved the workspace as you entered your function, you can now open MATLAB and load the saved workspace to see what data came into your function. You can then debug your function if needed from inside MATLAB. |