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

1

3

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:

Empty Matlab Command Window

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:

Manually typed command to the command window

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)

Percussion answered 11/3, 2012 at 12:35 Comment(0)
C
4

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.

Credential answered 11/3, 2012 at 18:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.