Google
 

Jan 31, 2008

Tips: Matlab混合编程

Matlab混合编程

Q: 如何在Java中调用Matlab?

R: 先用Matlab Builder for Java将Matlab函数化成Java类。R2006b之后版本支持。

[1] 参考1

[2] 参考2

Q: C#调用Matlab

R:

方案1: Matlab Engine,效率低,但方便,可以交互排错

方案2: Matlab Builder for NET ,似乎目前还不是很成熟

Q: BCC或者VC调用Matlab

R: Matlab Engine或Matlab Builder

Q: Matlab调用C

R: 参考mex文件编译帮助。需要按照matlab的要求编写mex接口文件,在matlab和C之间传递数据。要注意内存分配问题。成熟,效率高,很多matlab下软件都用这种方式实现性能提高。

Q: 我用CodeGear,编译无错,但link的时候有错

R: 设置该项目的LIB PATH,令CodeGear RAD能够成功找到需要链接的lib文件。或者在source文件中用编译指令直接指定进来:

#pragma link "libeng.lib"

或者

#pragma comment(lib, "libeng.lib")

Q: 运行错?

R:

原因1:可能有没有链接到的程序库。重新检查程序链接设置,确认所有用到的lib都被链接了进来;

原因2:找不到正确的DLL。最笨的办法是将可执行程序拷贝到matlab目录下去运行,也应该可以修改PATH环境变量。我在开发时则是直接设置Borland IDE Run => Parameters => Working Directory为Matlab目录...\matlab2007a\bin\win32实现的。

原因2:用matlab engine的本质是matlab要自动创建一个COM Server,这需要先在系统中注册才行。运行如下命令:

matlab /regserver

在系统中注册matlab COM server。

On Windows, engOpen creates an instance of a COM server, which is the MATLAB engine. You must have that server registered on your computer for it to work. On your second machine:
   1. open a command prompt by command "cmd".
   2. cd to the dir where matlab.exe lives. (not necessary if the path has already included in the %PATH%)
   3. execute the command "matlab /regserver" Now try running your app. This was in the engOpen help of the MATLAB docs.

Q: 调用Matlab,无法修改路径

具体是这样的:
#include <engine.h>
void main()
{
Engine* ep=0;
ep=engOpen(NULL); //打开matlab
engEvalString(ep,"huagui"); //huagui是自己编辑的一个指令
}
    当huagui.m这个文件在D;\matlab\work的时候,huagui能够运行。但是,当huagui.m在其他文件夹的时候,我也希望huagui能够运行。
    于是,我在engEvalString(ep,"huagui");语句前面添加了其他语句,希望更改matlab里的path……但是以下语句都无法起作用:
engEvalString(ep,"path(path,'E:\cfile\softwarepro\source')");
或者
engEvalString(ep,"cd('E:\cfile\softwarepro\source')");
或者
engEvalString(ep,"addpath('E:\cfile\softwarepro\source')");
    请问,您知道这个问题如何解决吗?(佚名)

R:  不是Matlab的问题,是你的程序有错误。
    应该使用这样的语法:
     engEvalString(ep,"path(path,'E:\\cfile\\softwarepro\\source')");
    或者
     engEvalString(ep,"cd('E:\\cfile\\softwarepro\\source')");
    或者
     engEvalString(ep,"addpath('E:\\cfile\\softwarepro\\source')");
    别忘了用“\\”表示路径。

No comments:

Google