Main Content

Call MATLAB from C

Run MATLAB® code from C programs, using mxArray

Engine applications are programs that allow you to call MATLAB from your own C/C++ programs, using MATLAB as a computation engine. To build a C engine application, call the mex function.

Note

The MATLAB Engine API for C++ is recommended over the Engine API for C. The MATLAB Engine API for C++ includes modern C++ features for writing engine applications. For more information, see Call MATLAB from C++. There are no plans to remove the Engine API for C.

Engine applications require an installed version of MATLAB; you cannot run the MATLAB engine on a machine that only has the MATLAB Runtime.

To build a C engine application, you need:

  • Experience writing C source code. You can create C source code files using Engine API for C and C Matrix API functions.

  • A compiler supported by MATLAB. For an up-to-date list of supported compilers, see Supported and Compatible Compilers.

  • Use the mex build script with the -client engine option.

Engine API for C

EngineType for MATLAB engine
engOpenStart MATLAB engine session
engOpenSingleUseStart MATLAB engine session for single, nonshared use
engCloseQuit MATLAB engine session
engEvalStringEvaluate expression in string
engGetVariableCopy variable from MATLAB engine workspace
engPutVariablePut variable into MATLAB engine workspace
engGetVisibleDetermine visibility of MATLAB engine session
engSetVisibleShow or hide MATLAB engine session
engOutputBufferSpecify buffer for MATLAB output

Topics

Requirements

Write Engine Applications

Build and Run Engine Applications

Troubleshooting

Can't Start MATLAB Engine

What to do when MATLAB engine does not run.

Debug MATLAB Function Called by C Engine

How to verify MATLAB functions used in engine applications.

User Input Not Supported

Some MATLAB functions that interact with the user are not supported in engine applications.

Multithreaded Applications

MATLAB libraries are not thread-safe.