C++ MEX Applications
Use the C++ MEX API to create MATLAB functions that provide the efficiency of C++ programs and that can access external functions and libraries from MATLAB. The C++ MEX API provides these features:
Uses modern C++ programming features and object-oriented designs.
Calls MATLAB functions from the MEX function.
Integrates seamlessly into MATLAB, getting inputs from and returning results to MATLAB.
Supports MATLAB data types and copy-on-write semantics.
Writes thread-safe multi-threaded MEX functions.
Executes C++ MEX functions out-of-process.
Functions in the MATLAB C++ APIs use the
matlab::data::Array class defined in the MATLAB Data API. If your MEX functions must run in MATLAB R2017b or earlier, or if you prefer to work in the C language, then use the C Matrix API which works with the
mxArray data structure instead. For more information, see Calling MATLAB from C.
|Base class for C++ MEX functions|
|Container for inputs and outputs from C++ MEX functions|
Design of C++ MEX Functions
Implement a class that overrides the function call operator
() to create a functor object.
Techniques to avoid making copies of large arrays to reduce memory usage and improve execution speed.
MexFunction class to better handle the resources used by your MEX function.
Check the size and type of inputs and display error messages in MATLAB.
MEX functions can display output in the MATLAB command window.
Behavior when calling back into MATLAB with an
async request like
Call MATLAB Functions
Call MATLAB functions, passing arguments from and returning arguments to a MEX function.
Catch exceptions thrown in a MEX function.
Execute MATLAB statements to create variables in the caller's workspace.
Separate Threads and Processes
MEX functions can call MATLAB from user-created threads using the C++ Engine asynchronous API.
Execute C++ MEX functions in processes that are separate from the MATLAB process.
Access MATLAB Variables
Put variables into or get variables from the MATLAB base and global workspaces.
Pass MATLAB objects to MEX functions.
You can use MEX functions to implement methods for MATLAB classes.