This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

C++ MEX Applications

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

Use C++ MEX 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:

  • Use modern C++ programming features and object-oriented designs.

  • Get inputs from MATLAB.

  • Return results to MATLAB.

  • Support MATLAB data types and copy-on-write semantics.

  • Call MATLAB functions from the MEX function.

  • Integrate seamlessly into MATLAB.

  • Write thread-safe multi-threaded MEX functions.

  • Execute C++ MEX functions out-of-process.

Note

The C++ MEX API supports modern C++ features and is not compatible with the C MEX API or the C Matrix API. You cannot mix the C++ MEX API with these APIs in a MEX file.

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.

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.