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)
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 engEvalStringengOpenSingleUse
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:
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.