| MATLAB® | ![]() |
| On this page… |
|---|
Calling MATLAB® Software from a C Application Calling MATLAB® Software from a C++ Application |
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.
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 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.c in the matlabroot\extern\examples\eng_mat directory. To see engdemo.cpp, open the file in the MATLAB Editor.
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 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.
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.
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.
Note On the UNIX®[1] platform, you cannot make an engine program connect to an existingMATLAB session. |
[1] UNIX is a registered trademark of The Open Group in the United States and other countries.
![]() | Using the MATLAB® Engine to Call MATLAB® Software from C and Fortran Programs | Compiling and Linking MATLAB® Engine Programs | ![]() |
| © 1984-2008- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |