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 the Supported and Compatible Compilers website.
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 using the
to call functions in shared libraries, see Call C Shared 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 write a MEX file to call a C function,
arrayProduct, in MATLAB using a MATLAB matrix.
This table links to source code for MEX examples.
This examples show different methods for adding data to an mxArray.
This example shows how to pass structures and cell arrays to MEX files.
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.
This example shows how to use unsigned 16-bit integers in a MEX file.
This example shows how to use C++ code with your C language MEX file.
This example shows C++ file handling features.
How to call a LAPACK or BLAS function using a MEX file.
Document your MEX file in a MATLAB script.
Print text in the MATLAB command window.
Print error information using the
How to exclude subclasses of built-in types from MEX file input arguments.
How to upgrade your MEX files.
MATLAB Version 8.4 (R2014b) changes the data type of handles
to graphics objects from
double to object.
MEX files are C/ C++ or Fortran subroutines you build and call from the command line.
Shows the components of a C/C++ source MEX file.
Use Matrix Library and MEX Library functions in your C/C++ and Fortran applications to interact with data in the MATLAB workspace.
Using mxArray in MEX files.
Shows how input and output arguments are represented in a MEX file.
How MEX files handle C/C++ strings.
How to manage memory in MEX files.
C++ language issues to consider when creating and using MEX files.
Use the 64-bit Matrix Library to build MEX files.
How to use 64-bit file I/O in your MEX file.