2012-03-11 192 views
3

我想发送简单的字符串命令到Matlab引擎。如何在我的代码中使用Matlab引擎(用于调用`engOpenSingleUse()`)?

这是我的代码(没有Matlab的API相关的代码,其他地方在我的代码,除了#include "engine.h"线):

void MatlabPlotter::DrawInMatlab() const 
{ 
    std::string PlotCommand = "x=[0 1 2 3 4 5];y=[0 1 4 9 16 25];plot(x, y);"; 
    void * vpDcom = NULL; 
    int iReturnValue; 
    engOpenSingleUse(PlotCommand.c_str(), vpDcom, &iReturnValue); 
} 

代码编译和运行成功,没有任何编译器错误或运行时错误消息。 “Matlab命令窗口”打开;我得到像下面的屏幕:

Empty Matlab Command Window

正如你看到的,命令窗口是空的。屏幕上没有绘图窗口。
当我手动键入命令到这个窗口,我得到的绘制,没有任何错误,象下面这样:

Manually typed command to the command window

这是engOpenSingleUse()功能的官方文档页面:
http://www.mathworks.com/help/techdoc/apiref/engopensingleuse.html

我在我的项目中添加了<MatlabInstallationDir>\extern\lib\win64\microsoft\libeng.lib库(我正在编译x64调试配置)。
我包含<MatlabInstallationDir>\extern\include\engine.h头文件。
我在主Matlab窗口输入!matlab /regserver命令(如engOpenSingleUse()函数的文档页面所述),以确保Matlab引擎已注册到我的操作系统。

为什么当我拨打engOpenSingleUse()函数时什么都不会发生?
当我在PlotCommand对象中发送字符串命令以绘制绘图图时,为什么不弹出绘图窗口?
我在做什么错?

操作系统:Windows 7旗舰版64位SP1,跟上时代的
IDE:Visual Studio 2010中,(版10.0.40219.1 SP1Rel)
Matlab的:7.8.0(R2009a)

回答

4

按文档你链接到,engOpenSingleUse的字符串参数是“开始”命令 - 这不是要执行的MATLAB命令。 engOpenSingleUse只是开始 MATLAB的引擎 - 你要调用不同的功能实际上使用发动机通过engEvalString

Engine* matlabEngine = engOpenSingleUse(0, vpDcom, &iReturnValue); 
engEvalString(matlabEngine, PlotCommand.c_str()); 

engOpenSingleUse只是意味着它开始只能由一个应用程序使用的引擎,它将执行一个命令字符串。

From the docs:

C语法

#include "engine.h" 
Engine *engOpenSingleUse(const char *startcmd, void *dcom, int *retstatus); 

参数:

startcmd字符串开始 MATLAB程序。在Microsoft Windows系统上,startcmd字符串必须为 为NULL。

dcom保留供将来使用;必须为NULL。

retstatus返回状态;可能的失败原因。

仅返回Microsoft Windows操作系统指向引擎 句柄的指针,如果打开失败,则返回NULL。

UNIX操作系统在UNIX系统上不支持。

为了完整起见,我会提及您应该检查以确保engOpen调用在继续执行程序之前返回非NULL指针。

相关问题