Write C++ Functions Callable from MATLAB (MEX Files) - MATLAB & Simulink (original) (raw)
Main Content
Create high-performance functions, implemented in modern C++, that can be called from MATLAB®
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 for C++. 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 Write C Functions Callable from MATLAB (MEX Files).
Build Function
mex | Build MEX function or engine application |
---|
C++ Classes
matlab::mex::Function | Base class for C++ MEX functions |
---|---|
matlab::mex::ArgumentList | Container for inputs and outputs from C++ MEX functions |
Out-of-Process Execution
mexhost | Create host process for C++ MEX function |
---|---|
feval | Evaluate C++ MEX function in MEX host process |
matlab.mex.MexHost | Out-of-process host for C++ MEX function execution |
Topics
Create C++ MEX Functions
- C++ MEX Functions
MEX files implement MATLAB functions written in C++. - Create a C++ MEX Source File
How to create a basic C++ MEX function.
Setup and Build MEX Files
- Build C++ MEX Programs
Build MEX applications on different platforms. - Test Your Build Environment
Test your build environment with a predefined C++ MEX file. - C++ MEX API
Use C++11 features and the C++ Engine API to implement MEX functions.
Design of C++ MEX Functions
- Structure of C++ MEX Function
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. - Manage External Resources from MEX Functions
Customize theMexFunction
class to better handle the resources used by your MEX function. - Handle Inputs and Outputs
Check the size and type of inputs and display error messages in MATLAB. - Display Output in MATLAB Command Window
MEX functions can display output in the MATLAB command window. - Make async Requests Using mexCallMATLAB
Behavior when calling back into MATLAB with anasync
request likematlab::engine::MATLABEngine::fevalAsync
. - Access ArgumentList and MATLAB Engine in User C++ Files
How to compilematlab::mex::ArgumentList
outside of MATLAB and link a shared library that uses MATLAB Engine API to a C++ MEX.
MATLAB Data Types in MEX Functions
- 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
- 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.
Separate Threads and Processes
- 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.
Access MATLAB Variables
- 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. - Use MEX Functions for MATLAB Class Methods
You can use MEX functions to implement methods for MATLAB classes.
Related Information
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
Belgium (English)
Denmark (English)
Deutschland (Deutsch)
España (Español)
Finland (English)
France (Français)
Ireland (English)
Italia (Italiano)
Luxembourg (English)
Netherlands (English)
Norway (English)
Österreich (Deutsch)
Portugal (English)
Sweden (English)
Switzerland
United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)