C Source MEX Files

Write C/C++ functions using MATLAB® mxArray API libraries


Functions in the C MEX APIs work with the MATLAB mxArray data structure, which is defined in the C Matrix API. To write applications using modern C++ features, see C++ MEX Applications.

A MEX file lets you call a C function from MATLAB. To create a C MEX file, you need:

For information about writing Fortran MEX files, see Fortran Source MEX Files.

For information about calling functions in shared libraries using the loadlibrary and calllib commands, 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.


Write C MEX Functions

Create C Source MEX File

This example shows how to create a C MEX function built with the C Matrix API.

Handle Complex Data

This example shows how to pass complex data to a MEX file using the C Matrix API.

Introducing MEX Files

MEX functions are C/ C++ or Fortran programs you build and call from the command line.

Components of MEX File

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.


Using mxArray in MEX files.

Typed Data Access

Typed data access functions in the C and Fortran Matrix APIs.

Data Flow in MEX Files

Shows how input and output arguments are represented in a MEX file.

Use Help Files with MEX Functions

Document your MEX function in a MATLAB script.

Handling Strings in C/C++

How MEX files handle C/C++ strings.

Handling Large File I/O

How to use 64-bit file I/O in your MEX file.

C MEX File Examples

Tables of MEX Function Source Code Examples

These tables link to source code for MEX function examples.

Fill mxArray

These examples show different methods for adding data to an mxArray.

Create 2-D Cell Array

This example shows how to create a cell array in a MEX file, using the mxcreatecellmatrix.c function, which places input arguments in a cell array.

Prompt User for Input

This example shows how to prompt a user for input.

User Messages

Print text in the MATLAB command window.

Error Handling

Print error information using the mexErrMsgIdAndTxt function.

Call LAPACK and BLAS Functions

How to call a LAPACK or BLAS function using a MEX file.

Testing for Most-Derived Class

How to exclude subclasses of built-in types from MEX file input arguments.

Write C++ MEX Functions

Creating C++ MEX Functions with C Matrix API

C++ language issues to consider when creating MEX functions built with the C Matrix API.

Use C++ Class in C MEX File

This example shows how to use C++ code with your C language MEX file.

Handle Files with C++

This example shows C++ file handling features.

Memory Management

Automatic Cleanup of Temporary Arrays

When a MEX function does not reach its normal return statement.

Persistent mxArrays

How to exempt an mxArray from automatic memory cleanup.

Related Information