![]() |
Platforms, Frameworks & Libraries »
COM / COM+ »
ActiveX
Intermediate
A class wrapper for Matlab(c) ActiveX ControlBy Jonathan de HalleuxEases up the use of the Matlab(c) COM server... |
VC6, VC7Win2K, WinXP, COM, Dev
|
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||

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
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();
Execute, PutMatrix, GetMatrix, PutString and GetString
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')"));
You can send array, real or complex, in the form of vectorPutMatrix.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
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.
Piece of cake using PutString:
mt.PutString("text", "Inserting a string named text");
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.
SetWorkspace. By default, the workspace is
"base". If you want to declare global variables, use "global" workspace.
mt.MinimizeWindow();
mt.MaximizeWindow();
mt.Show( true /* true to make it visible, false to hide it*/);
if(mt.IsVisivle()) // Matlab is server is visible...
mt.Quit();
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
PutCellArray and GetCellArray to write and read cell arrays.mlapp.tlb) in bin/win32 can give you the available methods implemented in the interface.| 21-10-2002 | Added references |
| 10-09-2002 |
|
| 09-25-2002 |
|
| 09-23-2002 |
|
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 21 Oct 2002 Editor: Chris Maunder |
Copyright 2002 by Jonathan de Halleux Everything else Copyright © CodeProject, 1999-2009 Web17 | Advertise on the Code Project |