Skip to Main Content Skip to Search
Product Documentation

Examples of Calling Engine Functions

Examples Overview

The matlabroot/extern/examples/eng_mat folder contains C, C++, and Fortran source code for examples demonstrating how to use the MATLAB engine. These examples create standalone programs. matlabroot represents the top-level folder where MATLAB is installed on your system.

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 Engine Applications with the MEX Command 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 standalone C program. For the Microsoft Windows version of this program, see engwindemo.c.

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

To see the Windows version engwindemo.c, open this 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 in the matlabroot\extern\examples\eng_mat folder. To see engdemo.cpp, open this file.

Calling MATLAB Software from a Fortran Application

The program, fengdemo.F, illustrates how to call the engine functions from a standalone Fortran program. To see the code, open this file.

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

On a Windows platform, you can attach an engine program to a MATLAB session that is already running by starting the MATLAB session with /Automation in the command line. When you call 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.

  


Recommended Products

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.

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