Main Content

Call MATLAB from C++

Run MATLAB® code from C++ programs, with object-oriented programming support and asynchronous execution

The MATLAB Engine API for C++ provides an interface between the C++ programming language and MATLAB. This API enables C++ programs to launch MATLAB, evaluate MATLAB functions with arguments, and exchange data between MATLAB and C++ programs. Supported operations include:

  • Start MATLAB.

  • Connect to a MATLAB shared session on the local machine.

  • Call MATLAB functions with input arguments passed from C++ and output variables returned from MATLAB.

  • Evaluate MATLAB statements in the MATLAB base workspace.

  • Pass variables from C++ to MATLAB and from MATLAB to C++.

To get started, see Set Up C++ Development Environment.

The MATLAB Engine API for C++ uses the MATLAB Data API for C++, which provides a way for applications running outside of MATLAB to work with MATLAB data through a language-neutral interface. The engine API also provides a strongly typed interface that enforces a strict data type mapping between MATLAB and C++. You can use this strongly typed interface to call MATLAB functions and classes by their MATLAB names in C++ as if they were native C++ functions and classes. For an example, see Integrate Strongly Typed MATLAB Data in C++ Application.

Classes

expand all

matlab::engine::MATLABEngineEvaluate MATLAB functions from C++ program
matlab::engine::FutureResultRetrieve result from asynchronous operation
matlab::engine::SharedFutureResult Retrieve result from asynchronous operation as shared future
matlab::engine::StreamBuffer Define stream buffer
matlab::engine::String Define UTF-16 string
matlab::engine::WorkspaceTypeType of MATLAB workspace

Functions

expand all

matlab::engine::startMATLABStart MATLAB synchronously
matlab::engine::startMATLABAsyncStart MATLAB asynchronously
matlab::engine::connectMATLABConnect to shared MATLAB session synchronously
matlab::engine::connectMATLABAsyncConnect to shared MATLAB session asynchronously
matlab::engine::findMATLABFind shared MATLAB sessions synchronously
matlab::engine::findMATLABAsyncFind shared MATLAB sessions asynchronously
matlab::engine::terminateEngineClientFree engine resources during run time
matlab::engine::convertUTF8StringToUTF16StringConvert UTF-8 string to UTF-16 string
matlab::engine::convertUTF16StringToUTF8StringConvert UTF-16 string to UTF-8 string
matlab.engine.shareEngineConvert running MATLAB session to shared session
matlab.engine.engineNameReturn name of shared MATLAB session
matlab.engine.isEngineSharedDetermine if MATLAB session is shared
matlab.engine.typedinterface.generateCPPGenerate C++ code interface for MATLAB namespaces, classes, and functions (Since R2022a)
mexBuild MEX function or engine application

Topics

Requirements

Integrate MATLAB Code in C++

Start and Connect to MATLAB

Call MATLAB Functions

Use MATLAB Data Types in C++

Related Information

Featured Examples