Click here to Skip to main content
15,860,861 members
Articles / Programming Languages / C++
Article

A class wrapper for Matlab(c) ActiveX Control

Rate me:
Please Sign up or sign in to vote.
4.42/5 (18 votes)
21 Oct 20023 min read 304.9K   3.9K   65   76
Eases up the use of the Matlab(c) COM server...

Sample Image - matlabengine.png

Introduction

Matlab(c) is a well-known scientific software covering a wide range of engineering and mathematicals fields. It contains also a set of complete and powerfull visualization tools.

Matlab(c) contains it's own language and you can easily develop applications on it. However, sometimes you will want to run it from a C/C++ application and that's where everything get's tricky. In fact, although Matlab(c) comes with a C API, it is complicated to use: linking problems, compiler special flags and other mysterious bugs (maybe I did it all wrong).

Another solution is to use the COM interface of the ActiveX control. The sad thing about this interface is that... it's COM and you end up with dozens of lines of code... That's where the CMatlabEngine gets in the play. It hides all the COM code (getting CLSID, getting IDispatch, allocating variants, setting function arguments and many others) and enables you to focus on the main part of the job: use Matlab(c).

This article will discuss the main features the class, for further details check the Doxygen documentation that is shipped with the demo project. Note that you can also generate it by running Doxygen on MatlabEngine.h

Initializing the engine

All the initialization code is contained in the constructor of CMatlabEngine. You can check that the server has been correctly initialized by calling IsInitialized:

// Initializing COM
CoInitialize(NULL);

...
// Hooking matlab engine
CMatlabEngine mt;
if (!mt.IsInitialized())
   // could not initialize matlab engine, do something about it

Do not forget to uninitialize COM when quitting the application:

CoUninitialize();

Engine main methods

The matlab engine contains 5 main methods: Execute, PutMatrix, GetMatrix, PutString and GetString

Executing Matlab(c) code:

You can execute Matlab(c) code by calling Execute( LPCTSTR szMatlabCode ):

// this code will show the classic Matlab(c) demo
mt.Execute(_T("surf(peaks)"));
mt.Execute(_T("title('demo')"));

Sending arrays to Matlab(c)

You can send array, real or complex, in the form of vector<double> to Matlab(c) by using PutMatrix.Note that the vector in PutMatrix have to be ordered line by lines: for a matrix M of size (mxn), M(i,j) is equivalent to M[i* n + j].

UINT nRows=10; // number of rows
UINT nCols=2;  // number of columns
vector<double> v( nRows * nCols );
// filling up v
...
// sending v to Matlab(c) with name "Mv"
mt.PutMatrix( _T("M"), v, nRows, nCols);
// M is now a matrix in the Matlab(c) workspace

Retreiving arrays from matlab

You can retreive arrays (real or complex) by calling GetMatrix( It is the dual of PutMatrix):

mt.Execute(_T("v=[1 2 3; 4 5 6]';"));
vector<double> vReal;
UINT nRows, nCols;
mt.GetMatrix(_T("v"), nRows, nCols, vReal);	

nRows and nCols now contains the dimension of the array and vReal the data.

Sending string to Matlab(c)

Piece of cake using PutString:

mt.PutString("text", "Inserting a string named text");

Getting string from Matlab(c)

Use the method GetString:

LPTSTR szText;
// we suppose that myText is a string variable in Matlab
mt.GetString("myText",szText);
Note that szText is allocated on the heap by GetString, the memory cleaning is left to the user.

Workspace

You can modify the workspace you are working on by using SetWorkspace. By default, the workspace is "base". If you want to declare global variables, use "global" workspace.

Handling the Matlab Window state

  • Minimize the command window,
    mt.MinimizeWindow();
  • Maximize the command window,
    mt.MaximizeWindow();
  • Show, hide the command window,
    mt.Show( true /* true to make it visible, false to hide it*/);
  • Get the visibility state the command window,
    if(mt.IsVisivle())
        // Matlab is server is visible...
  • Quit the command window,
    mt.Quit();

Known bugs and limitations

Matlab 5.3 and earlier

Some function interface from the Matlab engine have been introduced only with the version 6.0 therefore user with version earlier than 6.0 would have faced problem with the class.

If you have a Matlab earlier than v6.0, undefine the MATLAB_VERSION_6 constant that is at the beginning of MatlabEngine.h, it will disable the following functions: IsVisible, Show, PutString, GetString

Further developments

  • Write PutCellArray and GetCellArray to write and read cell arrays.

Reference and further reading

Revision History

21-10-2002Added references
10-09-2002
  • GetString is now working! At last !
  • Fixed bugs in GetResult, GetMatrix thanks to Juan Carlos Cobas.
09-25-2002
  • GetMatrix is now working! At last !
09-23-2002
  • Added PutString method
  • Added MATLAB_VERSION_6 string for Matlab version < 6. Thanks for ZHANG Yan for finding the workaround.

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.

A list of licenses authors might use can be found here


Written By
Engineer
United States United States
Jonathan de Halleux is Civil Engineer in Applied Mathematics. He finished his PhD in 2004 in the rainy country of Belgium. After 2 years in the Common Language Runtime (i.e. .net), he is now working at Microsoft Research on Pex (http://research.microsoft.com/pex).

Comments and Discussions

 
QuestionThanks Pin
gongjinnan31-May-12 16:40
gongjinnan31-May-12 16:40 
Generaldisp('hello'); Pin
xtlan16-Sep-09 13:07
xtlan16-Sep-09 13:07 
GeneralRe: disp('hello'); Pin
xtlan18-Sep-09 11:58
xtlan18-Sep-09 11:58 
GeneralPutMatrix() doesn't WORK Pin
53rg1003-Oct-07 0:26
53rg1003-Oct-07 0:26 
Generalfor superNoobs Pin
fdfszfsfds23-Apr-07 5:53
fdfszfsfds23-Apr-07 5:53 
GeneralFirst time Pin
Ovat1-Aug-06 12:12
Ovat1-Aug-06 12:12 
Questionmatlab 7 support? Pin
arnonm11-Jun-05 21:28
arnonm11-Jun-05 21:28 
AnswerRe: matlab 7 support? Pin
arnonm11-Jun-05 22:13
arnonm11-Jun-05 22:13 
GeneralRe: matlab 7 support? Pin
rdiaz329-Nov-05 0:13
rdiaz329-Nov-05 0:13 
GeneralRe: matlab 7 support? Pin
Andy Kuo21-Apr-06 11:06
Andy Kuo21-Apr-06 11:06 
GeneralRe: matlab 7 support? Pin
rdrouin29-May-06 4:09
rdrouin29-May-06 4:09 
GeneralGetMatrix \ SetMatrix Pin
Member 58343330-Dec-04 23:48
Member 58343330-Dec-04 23:48 
Generalmatlab error Pin
Member 51156316-Dec-03 22:55
Member 51156316-Dec-03 22:55 
GeneralRe: matlab error Pin
Jonathan de Halleux17-Dec-03 1:20
Jonathan de Halleux17-Dec-03 1:20 
Generalmatlab error Pin
Member 51156316-Dec-03 18:50
Member 51156316-Dec-03 18:50 
GeneralRe: matlab error Pin
Jonathan de Halleux16-Dec-03 21:22
Jonathan de Halleux16-Dec-03 21:22 
Generalsimple questions Pin
astridwahlin15-Dec-03 4:44
astridwahlin15-Dec-03 4:44 
GeneralRe: simple questions Pin
Jonathan de Halleux16-Dec-03 21:20
Jonathan de Halleux16-Dec-03 21:20 
QuestionNot releasing memory? Pin
arnonm4-Jun-03 1:22
arnonm4-Jun-03 1:22 
AnswerRe: Not releasing memory? My unvalidated solution Pin
arnonm4-Jun-03 2:06
arnonm4-Jun-03 2:06 
GeneralRe: Not releasing memory? My unvalidated solution Pin
Jonathan de Halleux4-Jun-03 3:20
Jonathan de Halleux4-Jun-03 3:20 
GeneralBug in CMatlabEngine::ErrHandler Pin
arnonm26-May-03 3:44
arnonm26-May-03 3:44 
GeneralRe: Bug in CMatlabEngine::ErrHandler Pin
arnonm26-May-03 21:14
arnonm26-May-03 21:14 
GeneralGood fix Pin
Jonathan de Halleux26-May-03 22:06
Jonathan de Halleux26-May-03 22:06 
GeneralThanks! Pin
arnonm22-May-03 0:39
arnonm22-May-03 0:39 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.