A MEX file lets you call a C function from MATLAB. To create a C MEX file, you need:
The ability to write C or C++ source code. You can create these files with the MATLAB Editor.
A compiler supported by MATLAB. For an up-to-date list of supported compilers, see Supported and Compatible Compilers.
mex build script. For more information,
see Executable C MEX Files.
For information about writing Fortran MEX files, see Fortran Source MEX Files.
For information about calling functions in shared libraries using the
see C Libraries.
For information about writing S-functions using C or C++ code, see your Simulink® documentation.
MEX files 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.
This example shows how to create a C MEX function built with the C Matrix API.
This example shows how to pass complex data to a MEX file using the C Matrix API.
MEX functions are C/ C++ or Fortran programs you build and call from the command line.
Shows the components of a C/C++ source MEX file.
Use Matrix API and MEX API functions in your C/C++ and Fortran applications to interact with data in the MATLAB workspace.
mxArray in MEX files.
Typed data access functions in the C and Fortran Matrix APIs.
Shows how input and output arguments are represented in a MEX file.
Document your MEX function in a MATLAB script.
How MEX files handle C/C++ strings.
How to use 64-bit file I/O in your MEX file.
These tables link to source code for MEX function examples.
These examples show different methods for adding data to an
This example shows how to create a cell array in a MEX file,
mxcreatecellmatrix.c function, which places input
arguments in a cell array.
This example shows how to prompt a user for input.
Print text in the MATLAB command window.
Print error information using the
How to call a LAPACK or BLAS function using a MEX file.
How to exclude subclasses of built-in types from MEX file input arguments.
C++ language issues to consider when creating MEX functions built with the C Matrix API.
This example shows how to use C++ code with your C language MEX file.
This example shows C++ file handling features.
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.