c to matlab2

Upload: riyaz-ahmed

Post on 10-Apr-2018

223 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/8/2019 c to Matlab2

    1/2

    07 Oct 04 revJCT

    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 dened functions within the Matlabinstance. Once an executable program is compiled, it may be run on any correct targetplatform 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 le stdafx.h must include the line.

    #include < matlab.h >

    4. The path to matlab.h must be included using Tools, Options, Directories, IncludeFiles 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 les isc : \ 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 les are required, tabei.h forexample, then the paths to the header and libraries must also be dened for proper linkingof the compiled source.

    III. MatlabEng wrapper class

    Although it is possible to make Matlab calls from c using only Mathworks libraries, a wrapperclass found on The Code Project website simplies the process by providing the classMatlabEng. The Code Project website is found at http: \\ www.codeproject.com. Thesource and sample Visual Studio project can be found there by searching for Matlab EngineAPI. You may be required to register to download the example project. The important lesto download are MatlabEng.cpp and MatlabEng.h. They may be saved anywhere. To usethe wrapper class, add the les to your project using File, Add to Project, Files from theIDE menu.

    1

  • 8/8/2019 c to Matlab2

    2/2

    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 le on local pc;);matlab.EvalString(matlab m le(argument););getch(); //remember to include conio.hmatlab.Close();

    The code snippet above is used to call a user dened (through creation of an m le)function. It is important to change the Matlab working folder to the location of the m leprior to attempting execution. If the argument is also a lename, it should reside in thecurrent Matlab working folder. No other tests were done to determine if les could be readfrom more than one directory. Note that not only can your own routines be called, butbuilt-in Matlab functions can also be used. Since data from the c code can be written to theMatlab workspace using the wrapper class PutVariable call, native Matlab functions can beused to operate on your data.

    CaveatYou may nd 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