Main Content

C++ MEX API

Note

The C++ MEX API is not compatible with the C MEX API described in Write C Functions Callable from MATLAB (MEX Files). You cannot mix these APIs in a MEX file.

The C++ MEX API enables you to create applications that take advantage of C++11 features, such as move semantics, exception handling, and memory management.

matlab::mex::Function Class

All MEX file implementations are classes that derive from matlab::mex::Function.

matlab::mex::Function Function

Description

getEngineGet pointer to the MATLABEngine object.
mexLockPrevent clearing of MEX file from memory.
mexUnlockAllow clearing of MEX file from memory.
getFunctionNameGet the name of the current MEX function.

matlab::mex::ArgumentList Class

The MEX function arguments passed with the operator() of the mex::Function class are matlab::mex::ArgumentList containers. ArgumentList is a full range to the underlying collection of arrays.

matlab::mex::ArgumentList Method

Description

operator[ ]Enables [] indexing into the elements of an ArgumentList.
beginBegin iterator.
endEnd iterator.
sizeReturns the number of elements in the argument list. Use this method to check the number of inputs and outputs specified at the call site.
emptyReturns a logical value indicating if the argument list is empty (size() == 0).

C++ Engine API

Access MATLAB® functions, variables, and objects using the matlab::engine::MATLABEngine class. To call methods in this class, use getEngine to create a shared pointer like matlabPtr in this example:

std::shared_ptr<matlab::engine::MATLABEngine> matlabPtr = getEngine();

Use matlabPtr to call engine methods. For example:

matlabPtr->feval(...);

Call engine methods only on the same thread as the mex::Function class.

matlab::engine::MATLABEngine Method

Description

Examples

feval

Evaluate MATLAB functions with input arguments synchronously. Use feval to pass arguments from C++ to MATLAB and to return a result from MATLAB to C++.

Call MATLAB Functions from MEX Functions
fevalAsync

Evaluate MATLAB functions with input arguments and returned values asynchronously.

For more information, see Making async Requests Using mexCallMATLAB.

eval

Evaluate a MATLAB statement as a string synchronously.

Execute MATLAB Statements from MEX Function
evalAsync

Evaluate a MATLAB statement as a string asynchronously.

For more information, see Making async Requests Using mexCallMATLAB.

getVariable

Get a variable from the MATLAB base or global workspace.

Set and Get MATLAB Variables from MEX
getVariableAsync

Get a variable from the MATLAB base or global workspace asynchronously.

 
setVariable

Put a variable into the MATLAB base or global workspace. If a variable with the same name exists in the MATLAB workspace, setVariable overwrites it.

Set and Get MATLAB Variables from MEX
setVariableAsync

Put a variable into the MATLAB base or global workspace asynchronously.

 
getProperty

Get the value of an object property.

MATLAB Objects in MEX Functions
getPropertyAsync

Get the value of an object property asynchronously.

 
setProperty

Set the value of an object property.

MATLAB Objects in MEX Functions
setPropertyAsync

Set the value of an object property asynchronously.

 

For information about exceptions, see MATLAB Engine API for C++ Exception Classes. For examples, see Catch Exceptions in MEX Function.

Related Topics