Examples of Calling Engine Functions

Overview

This section describes steps you must follow when using the engine functions. For example, before using engPutVariable, you must create a matrix and populate it.

After reviewing these examples, follow the instructions in Compiling and Linking MATLAB® Engine Programs to build the application and test it. You can test that your system is properly configured for engine applications by building and running an application.

Calling MATLAB® Software from a C Application

The program, engdemo.c, illustrates how to call the engine functions from a stand alone C program. For the Microsoft® Windows® version of this program, see engwindemo.c in the matlabroot\extern\examples\eng_mat directory. matlabroot is the MATLAB® root directory. Engine examples, like the MAT-file examples, are located in the eng_mat directory matlabroot\extern\examples\eng_mat.

To see engdemo.c, open the file in the MATLAB Editor.

To see the Windows version engwindemo.c, open the file.

The first part of this program launches MATLAB and sends it data. MATLAB analyzes the data and plots the results.

The figure is entitled position versus time for a falling object. The x axis is labeled time (seconds). The y axis is labeled position (meters).

The program continues with:

Press Return to continue

Pressing Return continues the program:

Done for Part I.
Enter a MATLAB command to evaluate.  This command should
create a variable X.  This program will then determine
what kind of variable you created.
For example: X = 1:5

Entering X = 17.5 continues the program execution.

X = 17.5
 
X =
 
   17.5000
 
 
Retrieving X...
X is class double
Done!

Finally, the program frees memory, closes the MATLAB engine, and exits.

Calling MATLAB® Software from a C++ Application

There is a C++ version of engdemo.c in the matlabroot\extern\examples\eng_mat directory. To see engdemo.cpp, open the file in the MATLAB Editor.

Calling MATLAB® Software from a Fortran Application

The program, fengdemo.F, illustrates how to call the engine functions from a stand alone Fortran program. To see the code, open the file in the MATLAB Editor.

Executing this program launches MATLAB, sends it data, and plots the results.

The figure is labeled position vs. time. The x axis is labeled time (seconds). The y axis is labeled position (meters).

The program continues with:

Type 0 <return> to Exit
Type 1 <return> to continue

Entering 1 at the prompt continues the program execution:

1
 MATLAB computed the following distances:
   time(s)  distance(m)
   1.00     -4.90    
   2.00     -19.6    
   3.00     -44.1    
   4.00     -78.4    
   5.00     -123.    
   6.00     -176.    
   7.00     -240.    
   8.00     -314.    
   9.00     -397.    
   10.0     -490.    

Finally, the program frees memory, closes the MATLAB engine, and exits.

Attaching to an Existing MATLAB® Session

You can make an engine program attach to a MATLAB session that is already running by starting the MATLAB session with /Automation in the command line. When you make a call to engOpen, it connects to this existing session. You should only call engOpen once, because any engOpen calls now connect to this one MATLAB session.

The /Automation option also causes the command window to be minimized. You must open it manually.

For example,

  1. Shut down any MATLAB sessions.

  2. From the Start button on the Windows menu bar, click Run.

  3. In the Open field, type:

    d:\matlab\bin\win32\matlab.exe /Automation

    or:

    d:\matlab\bin\win64\matlab.exe /Automation

    where d:\matlab\bin\win32 or d:\matlab\bin\win64 represents the path to the MATLAB executable.

  4. Click OK. This starts MATLAB.

  5. In MATLAB, change directories to matlabroot/extern/examples/eng_mat.

  6. Compile the engwindemo.c example.

  7. Run the engwindemo program by typing at the MATLAB prompt:

    !engwindemo

    This does not start another MATLAB session, but rather uses the MATLAB session that is already open.


[1] UNIX is a registered trademark of The Open Group in the United States and other countries.

  


 © 1984-2008- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS