Structure of C++ Engine Applications
A C++ engine application is a C++ program that integrates MATLAB® code and runs it using the MATLAB Engine API for C++. To create the C++ application, include these steps in your code. For examples, see Integrate MATLAB Function in C++ Application and Integrate Strongly Typed MATLAB Data in C++ Application.
Start MATLAB Engine
Start a MATLAB engine session from your C++ program synchronously or asynchronously.
To start the session, use one of these utility functions, which are defined in the
matlab::engine
namespace:
matlab::engine::startMATLAB
— Start a MATLAB session synchronously.matlab::engine::startMATLABAsync
— Start a MATLAB session asynchronously.
For an example, see Start MATLAB Session Synchronously.
Declare Variables
Within your C++ code, you can create a matlab::data::ArrayFactory
object and
use the member functions to create matlab::data::Array
objects, which
you can pass to functions. Choose a member function, such as createScalar
or createArray
, based on the data
type of the variable you want. For an example, see Call Function with Single Returned Argument.
The size of data arrays passed between C++ and MATLAB is limited to 2 GB. This limit applies to the data plus supporting information passed between the processes.
Call MATLAB Functions or Evaluate MATLAB Statements
To call MATLAB functions from C++, use the feval
and fevalAsync
member functions of
the matlab::engine::MATLABEngine
class.
Use these functions when you want to pass function arguments from C++ to MATLAB and to return the result of the function execution to C++. These
member functions work like the MATLAB
feval
function. For more
information, see Call MATLAB Functions from C++.
To evaluate MATLAB statements from C++, use the eval
and evalAsync
member functions of
the matlab::engine::MATLABEngine
class.
These member functions are similar to the MATLAB
eval
function. The
eval
and evalAsync
member functions do
not return the results of evaluating the MATLAB statement. For more information, see Evaluate MATLAB Statements from C++.
Handle Exceptions
When the MATLAB Engine API for C++ encounters an error, such as a MATLAB syntax error or a run-time error, the engine API throws an exception of one of these types. You can catch and handle these exceptions in your C++ code.
Exception | Cause |
---|---|
| Base class of all C++ engine exceptions. |
| There is a MATLAB run-time error in function or MATLAB fails to start. |
| The MATLAB session is not available |
| There is a syntax error in the MATLAB function. |
| There is a MATLAB run-time error in the MATLAB function or statement. |
| Evaluation of the MATLAB function is canceled. |
| Thrown by matlab::engine::FutureResult::get if
the evaluation of the MATLAB function or statement is interrupted. |
| The result of the MATLAB function cannot be converted to the specified type |
Clean Up and Exit
Clean up variables, close files, free resources associated with the engine API,
and exit. Make sure to terminate the MATLAB session using the TerminateEngineClient
function.