Main Content

Choosing MEX Applications

R2026b

You can call your own C, C++, or Fortran programs from the MATLAB® command line as if they were built-in functions. These programs are called MEX functions. MEX functions are not appropriate for all applications. MATLAB is a high-productivity environment whose specialty is eliminating time-consuming, low-level programming in compiled languages like C or C++. In general, do your programming in MATLAB.

Note

To directly call functions in C/C++ libraries with MATLAB R2023a or later, see clibPublishInterfaceWorkflow.

To create a MEX function, write your program using MATLAB APIs, then build it using the mex command. The APIs provide these features:

  • Call MATLAB functions from the MEX function.

  • Integrate seamlessly into MATLAB, getting inputs from and returning results to MATLAB.

  • Support MATLAB data types.

MEX Terms

MEX stands for MATLAB executable and has different meanings, as shown in this table.

MEX TermDefinition
MEX fileC, C++, or Fortran source code file.
MEX functionDynamically linked subroutine executed in the MATLAB environment.
MEX APIFunctions in the C MEX API and Fortran MEX API to perform operations in the MATLAB environment.
mex build scriptMATLAB function to create a binary file from a source file.

C++ MEX Functions

Write your C++ MEX functions using the MATLAB Data API for C++ and the C++ MEX API, which support C++11 programming features. These APIs, based on the matlab::data::Array class, provide better type safety, array bounds checking, and support for modern C++ constructs to simplify coding. The APIs:

  • Improves performance by supporting shared data and in-place optimization.

  • Supports native C++ types which make it easier to create MATLAB data arrays. The feval function for calling MATLAB from C++ supports primitive C++ types and natural argument lists, while the C function mexCallMATLAB requires making an array of inputs where each input must be an mxArray.

  • Improves robustness by supporting iterators and smart pointers instead of pointer arithmetic on raw pointers. This reduces the chance for memory corruption by removing direct access to the data pointers.

  • Supports row and column major data making it easier to adapt to C/C++ row-major data.

For more information, see Write C++ Functions Callable from MATLAB (MEX Files).

C/C++ MEX Functions for MATLAB R2017b and Earlier

If your MEX functions must run in MATLAB R2017b or earlier, or if you prefer to work in the C language, then write your source files using functions in these libraries based on the mxArray data structure. For more information, see Write C Functions Callable from MATLAB (MEX Files).

Caution

Do not mix functions in the C Matrix API with functions in the MATLAB Data API.

Fortran MEX Functions

To write Fortran MEX functions, use the Fortran MEX API and the Fortran Matrix API based on the mxArray data structure.

For more information, see Write Fortran Functions Callable from MATLAB (MEX Files).

See Also

| | |