Sie sind auf Seite 1von 2

07 Oct 04 rev

JCT

Calling Matlab from c


I. Overview
Matlab is distributed with libraires that allow a user to launch an instance of Matlab,
exchange data with Matlab and use built-in and user defined functions within the Matlab
instance. Once an executable program is compiled, it may be run on any correct target
platform providing the Matlab libraries exist on the target pc.

II. Requirements
1. Matlab must be installed on the pc on which the program is being compiled.

2. The Microsoft Visual C++ IDE must be used. It is the only IDE tested.

3. The precompiled header file stdafx.h must include the line.

#include <matlab.h>

4. The path to matlab.h must be included using “Tools, Options, Directories, Include
Files” in the IDE. A typical path to matlab.h is c : \matlab6p5\extern\include.

5. The path to the precompiled matlab libraries must be included using “Tools, Op-
tions, Directories, Library Files” in the IDE. A typical path to the library files is
c : \matlab6p5\extern\lib\win32\microsoft\msvc50.

6. The path to MatlabEng.h (described in the next section) must be included using “Tools,
Options, Directories, Include Files” in the IDE.

If any other precompiled libraries and corresponding header files are required, tabei.h for
example, then the paths to the header and libraries must also be defined for proper linking
of the compiled source.

III. MatlabEng wrapper class


Although it is possible to make Matlab calls from c using only Mathworks libraries, a wrapper
class found on ‘The Code Project’ website simplifies the process by providing the class
MatlabEng. The Code Project website is found at http : \\www.codeproject.com. The
source and sample Visual Studio project can be found there by searching for ‘Matlab Engine
API’. You may be required to register to download the example project. The important files
to download are MatlabEng.cpp and MatlabEng.h. They may be saved anywhere. To use
the wrapper class, add the files to your project using ‘File, Add to Project, Files’ from the
IDE menu.

1
IV. C code sample
To use the MatlabEng class in your code, include the line

#include “MatlabEng.h”

in your c or c++ source that is calling Matlab and refer to the following sample...

CMatlabEng matlab;
matlab.Open(NULL);
matlab.SetVisible(TRUE);
matlab.EvalString(”cd path to an m file on local pc;”);
matlab.EvalString(”matlab m file(argument);”);
getch(); //remember to include conio.h
matlab.Close();

The code snippet above is used to call a user defined (through creation of an m file)
function. It is important to change the Matlab working folder to the location of the m file
prior to attempting execution. If the argument is also a filename, it should reside in the
current Matlab working folder. No other tests were done to determine if files could be read
from more than one directory. Note that not only can your own routines be called, but
built-in Matlab functions can also be used. Since data from the c code can be written to the
Matlab workspace using the wrapper class PutVariable call, native Matlab functions can be
used to operate on your data.

Caveat—You may find that even though your source compiles and links without error,
you receive messages like

‘a device attached to the system is not functioning’

when you execute yor code. This message can be generated by the lapack.dll Matlab dynamic

Das könnte Ihnen auch gefallen