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.
Note
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.
MEX files implement MATLAB functions written in C++.
Here is how to create a basic C++ MEX function.
Build MEX applications on different platforms.
Test your build environment with a predefined C++ MEX file.
Use C++11 features and the C++ Engine API to implement MEX functions.
Implement a class that overrides the function call operator ()
to create a functor object.
Avoid Copies of Arrays in MEX Functions
Techniques to avoid making copies of large arrays to reduce memory usage and improve execution speed.
Managing External Resources from MEX Functions
Customize the 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.
Displaying Output in MATLAB Command Window
MEX functions can display output in the MATLAB command window.
Making async Requests Using mexCallMATLAB
Behavior when calling back into MATLAB with an async
request like matlab::engine::MATLABEngine::fevalAsync
.
Data Access in Typed, Cell, and Structure Arrays
Cell and structure arrays let you write to array contents without copying entire array.
Data Types for Passing MEX Function Data
Define the correct data types to pass data between MATLAB and MEX functions.
Call MATLAB Functions from MEX Functions
Call MATLAB functions, passing arguments from and returning arguments to a MEX function.
Catch Exceptions in MEX Function
Catch exceptions thrown in a MEX function.
Execute MATLAB Statements from MEX Function
Execute MATLAB statements to create variables in the caller's workspace.
Call MATLAB from Separate Threads in MEX Function
MEX functions can call MATLAB from user-created threads using the C++ Engine asynchronous API.
Out-of-Process Execution of C++ MEX Functions
Execute C++ MEX functions in processes that are separate from the MATLAB process.
Set and Get MATLAB Variables from MEX
Put variables into or get variables from the MATLAB base and global workspaces.
MATLAB Objects in MEX Functions
Pass MATLAB objects to MEX functions.
Using MEX Functions for MATLAB Class Methods
You can use MEX functions to implement methods for MATLAB classes.