MATLAB Engine

MATLAB provides some API functions for controlling it. These APIs required that you installed MATLAB on your target machine. We named these APIs, MATLAB Engine API. For an alternative method, you can use MATLAB ActiveX control. For more information refer to article: A class wrapper for Matlab (C) ActiveX Control by Jonathan de Halleux.

CMatlabEng in detail

Table 1 is a complete reference of member functions of CMatlabEng class.

Member Function

Description

int OutputBuffer(char *p, int n);

OutputBuffer defines a character buffer for EvalString to return any output that ordinarily appears on the screen.The default behavior of EvalString is to discard any standard output caused by the command it is executing. OutputBuffer (p,n) tells any subsequent calls to EvalString to save the first n characters of output in the character buffer pointed to by p. To turn off output buffering, use OutputBuffer(ep,NULL,0);

void OpenSingleUse (constchar *startcmd, void *dcom, int *retstatus);

This routine allows you to start multiple MATLAB processes for the purpose of using MATLAB as a computational engine. OpenSingleUse starts a MATLAB process, establishes a connection, and returns a unique engine identifier, or NULL if the open fails. OpenSingleUse starts a new MATLAB process each time it is called.OpenSingleUse opens a COM channel to MATLAB. This starts the MATLAB that was registered during installation. If you did not register during installation, on the command line you can enter the command: matlab /regserverOpenSingleUse allows single-use instances of a MATLAB engine server. OpenSingleUse differs from Open, which allows multiple users to use the same MATLAB engine server.

int GetVisible (bool* value);

Returns status of the window for the MATLAB engine session, is visible or invisible on the Windows desktop.

int SetVisible (boolvalue);

SetVisible makes the window for the MATLAB engine session, either visible or invisible on the Windows desktop. You can use this function to enable or disable user interaction with the MATLAB engine session.

mxArray* GetVariable (constchar* name);

Reads the named mxArray from the MATLAB engine session associated with ep and returns a pointer to a newly allocated mxArray structure, or NULL if the attempt fails. GetVariable fails if the named variable does not exist.Be careful in your code to free the mxArray created by this routine when you are finished with it.

int PutVariable (constchar *name, const mxArray *mp);

PutVariable writes mxArray mp to the engine ep, giving it the variable name, name. If the mxArray does not exist in the workspace, it is created. If an mxArray with the same name already exists in the workspace, the existing mxArray is replaced with the new mxArray.

int EvalString (constchar* string);

Evaluates the expression contained in string for the MATLAB engine session, previously started by Open.

void Open (constchar* StartCmd);

This routine allows you to start a MATLAB process for the purpose of using MATLAB as a computational engine.

int Close();

This routine allows you to quit a MATLAB engine session.

Sample Program

Following program is a sample that uses CMatlabEng class. Program logic is very simple.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

Share

About the Author

I was born in Shiraz, a very beautiful famous city in Iran. I started programming when I was 12 years old with GWBASIC. Since now, I worked with various programming languages from Basic, Foxpro, C/C++, Visual Basic, Pascal to MATLAB and now Visual C++.
I graduated from Iran University of Science & Technology in Communication Eng., and now work as a system programmer for a telecommunication industry.
I wrote several programs and drivers for Synthesizers, Power Amplifiers, GPIB, GPS devices, Radio cards, Data Acqusition cards and so many related devices.
I'm author of several books like Learning C (primary and advanced), Learning Visual Basic, API application for VB, Teach Yourself Object Oriented Programming (OOP) and etc.
I'm winner of January, May, August 2003 and April 2005 best article of month competetion, my articles are:

Comments and Discussions

i tried this:
***********************
char buffer[100] = "";
matlab.OutputBuffer(buffer, 100);
cout << "Output: " << buffer << endl;
***********************
to get the matlab outputbuffer but the variable buffer is empty at any place in my code . can somebody help me understand this function?
thank you