Click here to Skip to main content
13,300,654 members (52,932 online)
Click here to Skip to main content
Add your own
alternative version


65 bookmarked
Posted 15 Sep 2002

A class wrapper for Matlab(c) ActiveX Control

, 21 Oct 2002
Rate this:
Please Sign up or sign in to vote.
Eases up the use of the Matlab(c) COM server...
<!-- Download Links --> <!-- Article image -->

Sample Image - matlabengine.png


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

// 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:


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

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
Note that szText is allocated on the heap by GetString, the memory cleaning is left to the user.


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,
  • Maximize the command window,
  • Show, hide the command window,
    mt.Show( true /* true to make it visible, false to hide it*/);
  • Get the visibility state the command window,
        // Matlab is server is visible...
  • Quit the command window,

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
  • GetString is now working! At last !
  • Fixed bugs in GetResult, GetMatrix thanks to Juan Carlos Cobas.
  • GetMatrix is now working! At last !
  • Added PutString method
  • Added MATLAB_VERSION_6 string for Matlab version < 6. Thanks for ZHANG Yan for finding the workaround.


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


About the Author

Jonathan de Halleux
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 (

You may also be interested in...


Comments and Discussions

GeneralRe: mxCreateCharArray Pin
Jonathan de Halleux7-Apr-03 0:09
memberJonathan de Halleux7-Apr-03 0:09 
GeneralRe: mxCreateCharArray Pin
A. Riazi28-May-03 0:59
memberA. Riazi28-May-03 0:59 
Questiongetstring? Pin
mansour_ahmadian9-Jan-03 4:57
membermansour_ahmadian9-Jan-03 4:57 
AnswerLook deeper... Pin
Jonathan de Halleux27-Jan-03 2:50
memberJonathan de Halleux27-Jan-03 2:50 
Generali hope you can help me!!!!! Pin
Sunnygirl8-Jan-03 9:28
memberSunnygirl8-Jan-03 9:28 
GeneralRe: i hope you can help me!!!!! Pin
mhossny30-Jan-03 5:22
membermhossny30-Jan-03 5:22 
Questionwhy use the ActiveX interfcae? Pin
Yosi Keller22-Oct-02 1:04
sussYosi Keller22-Oct-02 1:04 
AnswerAn answer Pin
Jonathan de Halleux22-Oct-02 2:08
memberJonathan de Halleux22-Oct-02 2:08 
I do not pretend this wrapper is better that the engine DLL. Maybe the text of the article is badly written. In fact, the engine dll has more feature that the COM component: eng..., mat... functions, etc...

However, when using the engine dll or mat dll, you link your project with the right libraries and compile it with some Matlab "mex.bat" file. With the COM interface, you don't need to know all that...

Jonathan de Halleux, Belgium.

GeneralRe: An answer Pin
mhossny30-Jan-03 5:26
membermhossny30-Jan-03 5:26 
GeneralAnyone have any examples of GetResult() Pin
laughinglizard20-Oct-02 12:49
memberlaughinglizard20-Oct-02 12:49 
GeneralRe: Anyone have any examples of GetResult() Pin
Jonathan de Halleux20-Oct-02 20:55
memberJonathan de Halleux20-Oct-02 20:55 
GeneralRe: Anyone have any examples of GetResult() Pin
laughinglizard21-Oct-02 4:56
memberlaughinglizard21-Oct-02 4:56 
GeneralYou are confused Pin
Jonathan de Halleux21-Oct-02 5:52
memberJonathan de Halleux21-Oct-02 5:52 
Generalsome problems Pin
Anonymous15-Oct-02 1:53
sussAnonymous15-Oct-02 1:53 
GeneralAutmation server error ? Someone with Matlab 5.2 help ! Pin
Jonathan de Halleux15-Oct-02 3:55
memberJonathan de Halleux15-Oct-02 3:55 
GeneralRe: Autmation server error ? Someone with Matlab 5.2 help ! Pin
Anonymous15-Oct-02 10:23
sussAnonymous15-Oct-02 10:23 
GeneralOne more question Pin
Juan Carlos Cobas8-Oct-02 0:05
memberJuan Carlos Cobas8-Oct-02 0:05 
GeneralAnother answer Pin
Jonathan de Halleux8-Oct-02 3:41
memberJonathan de Halleux8-Oct-02 3:41 
GeneralRe: Another answer Pin
Juan Carlos Cobas8-Oct-02 6:10
memberJuan Carlos Cobas8-Oct-02 6:10 
GeneralRe: Another answer Pin
Jonathan de Halleux8-Oct-02 6:13
memberJonathan de Halleux8-Oct-02 6:13 
GeneralRe: Another bug Pin
Juan Carlos Cobas8-Oct-02 8:52
memberJuan Carlos Cobas8-Oct-02 8:52 
GeneralFixed Pin
Jonathan de Halleux9-Oct-02 0:37
memberJonathan de Halleux9-Oct-02 0:37 
GeneralMinor problem Pin
Juan Carlos Cobas25-Sep-02 2:12
memberJuan Carlos Cobas25-Sep-02 2:12 
GeneralRe: Minor problem Pin
Jonathan de Halleux25-Sep-02 2:25
memberJonathan de Halleux25-Sep-02 2:25 
Generalgreat job Pin
Anonymous19-Sep-02 22:26
sussAnonymous19-Sep-02 22:26 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.171207.1 | Last Updated 22 Oct 2002
Article Copyright 2002 by Jonathan de Halleux
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid