C++ MEX API - MATLAB & Simulink (original) (raw)

Main Content

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
getEngine Get pointer to the MATLABEngine object.
mexLock Prevent clearing of MEX file from memory.
mexUnlock Allow clearing of MEX file from memory.
getFunctionName Get the name of the current MEX function.

matlab::mex::ArgumentList Class

The MEX function arguments passed with the operator() of themex::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 anArgumentList.
begin Begin iterator.
end End iterator.
size Returns the number of elements in the argument list. Use this method to check the number of inputs and outputs specified at the call site.
empty Returns 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:

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

For information about exceptions, see Handle Exceptions. For examples, see Catch Exceptions in MEX Function.