Main Content

C++ MEX Applications

Create high-performance MATLAB® functions implemented in modern C++

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.

C++ Classes

matlab::mex::FunctionBase class for C++ MEX functions
matlab::mex::ArgumentListContainer for inputs and outputs from C++ MEX functions

Out of Process Execution

mexhostCreate host process for C++ MEX function
fevalEvaluate C++ MEX function in MEX host process
matlab.mex.MexHostOut-of-process host for C++ MEX function execution

Topics

Begin Creating C++ MEX Functions

C++ MEX Functions

MEX files implement MATLAB functions written in C++.

Create a C++ MEX Source File

Here is 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.

Managing External Resources from MEX Functions

Customize the MexFunction class to better handle the resources used by your MEX function.

Handling Inputs and Outputs

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.

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.

Using MEX Functions for MATLAB Class Methods

You can use MEX functions to implement methods for MATLAB classes.