Choosing MEX Applications
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. Do not use MEX files unless your application requires it.
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.
C++ MEX Functions
As of MATLAB R2018a, write your C++ MEX functions using these APIs, 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.
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 these APIs based on the mxArray
data structure.
For more information, see Write Fortran Functions Callable from MATLAB (MEX Files).
MEX Terms
MEX stands for MATLAB executable and has different meanings, as shown in this table.
MEX Term | Definition |
---|---|
source MEX file | C, C++, or Fortran source code file. |
binary MEX file or MEX function | Dynamically linked subroutine executed in the MATLAB environment. |
MEX API | Functions in the C MEX API and Fortran MEX API to perform operations in the MATLAB environment. |
mex build script | MATLAB function to create a binary file from a source file. |
See Also
mxArray
| matlab::data::Array
| mex