You can call your own C or C++ programs from the MATLAB command line as if they were built-in functions. These programs are called MEX functions and the function name is the MEX file name. 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. In general, do your programming in MATLAB. Do not use MEX functions unless your application requires it.
To create a MEX function, write your programs using MATLAB APIs. The functions in these libraries facilitate the transfer of data between MEX functions and the workspace. To choose a MATLAB API, consider the following.
If your MEX functions must run in MATLAB R2017b or earlier, or if you prefer to work in the C
language, then use functions in the C Matrix API and the C MEX API.
These APIs use the MATLAB
structure. The functions and topics on this page are based on
Choose functions from either the C Matrix API or the MATLAB Data API; you cannot mix functions from these APIs.
Build your source file into an executable program using the
mex function. You can also share
the MEX file with other MATLAB users.
For information about writing S-functions, see your Simulink® documentation.
To call a MEX function that someone else created, see MEX File Functions.
If you have a C/C++ program and want to call MATLAB functions from the program, then use one of the engine APIs.
If you want to read and write MATLAB data from C/C++ programs, then use the MATLAB C API to Read MAT-File Data.
|Entry point to C/C++ MEX function built with C Matrix API|
|Name of current MEX function|
|Register function to call when MEX function clears or MATLAB terminates|
|Call MATLAB function, user-defined function, or MEX function|
|Call MATLAB function, user-defined function, or MEX file and capture error information|
|Execute MATLAB command in caller workspace|
|Execute MATLAB command in caller workspace and capture error information|
|Copy of variable from specified workspace|
|Read-only pointer to variable from another workspace|
|Array from MEX function into specified workspace|
|Value of specified graphics property|
|Set value of specified graphics property|
|ANSI C PRINTF-style output routine|
|Display error message with identifier and return to MATLAB prompt|
|Warning message with identifier|
|Determine if MEX file is locked|
|Prevent clearing MEX file from memory|
|Allow clearing MEX file from memory|
|Make array persist after MEX file completes|
|Make memory allocated by MATLAB persist after MEX function completes|
This example shows how to create the
arrayProduct C MEX
function built with the C Matrix API.
Links to source code for MEX function examples.
mxArray in MEX files.
Rules for managing
C++ language issues to consider when creating MEX functions built with the C Matrix API.
Print error information using the
How to use 64-bit file I/O in your MEX file.
How to exclude subclasses of built-in types from MEX file input arguments.
MATLAB Version 9.4 (R2018a) supports an interleaved representation of complex numbers.
MATLAB Version 9.2 (R2017a) builds MEX files with the 64-bit API by default.
MATLAB Version 8.4 (R2014b) changes the data type of handles to graphics objects
double to object.
This example shows how to document the
MEX file built on a Windows® platform using a Microsoft®
Visual C++® compiler.
To help diagnose compiler set up and build errors, call the
mex function with verbose option -v.
What to do when compiling a MEX file fails.
More information for warnings and error messages.
Troubleshooting MEX files built with the MinGW-w64 compiler.
Every MEX file needs a
What to do when you get an invalid MEX file error.
For best results, run MEX files built with your MATLAB version.
If you get a binary MEX file from another source, be sure that the file was compiled for the same platform on which you want to run it.
Do not call a single session of MATLAB on separate threads from a MEX file.
What to do when a MEX file causes a segmentation violation or assertion.
What to do when your MEX generates wrong answers.