| Contents | Index |
| On this page… |
|---|
Calling MATLAB Software from a C Application Calling MATLAB Software from a C++ Application |
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.
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 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.
There is a C++ version of engdemo in the matlabroot\extern\examples\eng_mat folder. To see engdemo.cpp, open this file.
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 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.
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.
Note For more information on the /Automation command-line argument, see Additional Automation Server Information. For information about the Component Object Model interfaces used by MATLAB, see Introducing MATLAB COM Integration. |
For example,
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.
In MATLAB, change directories to matlabroot/extern/examples/eng_mat.
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.
![]() | Using MATLAB Engine | Compiling Engine Applications with the MEX Command | ![]() |

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 |