How do I use Matlab engine in my code (for calling `engOpenSingleUse()`)?

戏子无情 提交于 2019-12-31 01:58:24

问题


I'm trying to send simple string commands to the Matlab engine.

This is my code (there is no Matlab API related code anywhere else in my code, except for the #include "engine.h" line):

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);
}

The code compiles and runs successfully without any compiler errors or run time error messages. The "Matlab Command Window" opens; I get a screen like below:

As you see, the command window is empty. There is no plotting window on the screen.
When I manually type the command into this window, I get the plotting without any error, like below:

This is the official documentation page for the engOpenSingleUse() function:
http://www.mathworks.com/help/techdoc/apiref/engopensingleuse.html

I added <MatlabInstallationDir>\extern\lib\win64\microsoft\libeng.lib library in my project (I'm compiling in x64 debug configuration).
I included <MatlabInstallationDir>\extern\include\engine.h header file.
I typed !matlab /regserver command to the main Matlab window (as described in the documentation page of the engOpenSingleUse() function) to make sure Matlab engine is registered to my OS.

Why doesn't anything happen when I call the engOpenSingleUse() function?
Why doesn't a plotting window pop up when I send the string command in the PlotCommand object to plot the plotting?
What am I doing wrong?

OS: Windows 7 Ultimate x64 SP1, up-to-date
IDE: Visual Studio 2010, (Version 10.0.40219.1 SP1Rel)
Matlab: 7.8.0 (R2009a)


回答1:


As per the documentation that you linked to, the string argument to engOpenSingleUse is the "start" command - this is NOT a MATLAB command to be executed. engOpenSingleUse just starts the MATLAB engine- you have to call a different function to actually use the engine via engEvalString

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

engOpenSingleUse just means the engine it starts can only be used by one application, not that it is going to execute a single command string.

From the docs:

C Syntax

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

Arguments:

startcmd String to start MATLAB process. On Microsoft Windows systems, the startcmd string must be NULL.

dcom Reserved for future use; must be NULL.

retstatus Return status; possible cause of failure.

Returns Microsoft Windows Operating Systems Only Pointer to an engine handle, or NULL if the open fails.

UNIX Operating Systems Not supported on UNIX systems.

For completeness, I'll mention that you should also check to make sure the engOpen call returned a non-NULL pointer before continuing with your program.



来源:https://stackoverflow.com/questions/9654965/how-do-i-use-matlab-engine-in-my-code-for-calling-engopensingleuse

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!