所有源文件都在空间中可以找到,主要有以下几个:
MatlabEngine.h
MatlabEngine.cpp
ImxArray.h
ImxArray.cpp
xArray.h(no cpp)
xBool.h
xBool.cpp
xDouble.h(no cpp)
xFile.h
xFile.cpp
xInt.h(no cpp)
xMatrix.h(no cpp)
xString.h
xString.cpp
xStruct.h
xStruct.cpp
要想进行开发,首先安装matlab和VC,我的版本是matlab09和V6.更高的版本我没试过。
当你得到上述文件后就可以使用了,下面以常规的交互为例子:
例子1:
矩阵 A=[ 1.2 7.5 12.3 ;4.1 3.1 6.1;10.2 10.2 -3.2]; 计算 A逆 和 det(A)
1.新建空工程win32 console工程sample1
2.新建test.cpp
3.把上述文件复制到工程目录下,并添加到工程中去。
3.在test中写代码如下:
#include <iostream.h> #include "xMatrix.h" #include "xDouble.h" #include "MatlabEngine.h" void main() { double a[]= { 1.2 , 7.5 ,12.3 , 4.1 , 3.1 ,6.1, 10.2 ,10.2 ,-3.2 }; xMatrixDouble::C2Mat(a,3,3,a); CMatlabEngine g(0);//gobal eng if(g.OpenEngine()) { mxWrap A(3,3,a); g.PutVar("A",A.GetArray()); g.EvalString("B=inv(A);C=det(A);"); mxWrap B,C; g.GetVar("B",&B); g.GetVar("C",&C); xMatrixDouble*pB=(xMatrixDouble*)B.GetArrayInterface(); xDouble*pC=(xDouble*)C.GetArrayInterface(); cout<<"B=inv(A):/n"; for (int i=0;i<pB->GetR();i++) { for (int k=0;k<pB->GetC();k++) { cout<<pB->GetRealAt(i,k)<<" "; } cout<<endl; } cout<<"C=det(A):/n"; cout<<pC->GetRealData()<<endl; g.CloseEngine();//CLOSE } }
4.编译运行如下:
如果编译错误,请检查编程环境设置。
san 2011.3. NUAA