This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materials including this page, select Japan from the country navigator on the bottom of this page.

C Source MEX Files

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

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 using the loadlibrary and calllib commands 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.

Examples and How To

Create C Source MEX File

This example shows how to write a MEX file to call a C function, arrayProduct, in MATLAB using a MATLAB matrix.

Table of MEX File Source Code Files

This table links to source code for MEX examples.

Fill mxArray

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

Pass Structures and Cell Arrays

This example shows how to pass structures and cell arrays to MEX files.

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.

Handle 8-, 16-, and 32-Bit Data

This example shows how to use unsigned 16-bit integers in a MEX file.

C++ Class in MEX Files

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.

Call LAPACK and BLAS Functions

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

Use Help Files with MEX Files

Document your MEX file in a MATLAB script.

User Messages

Print text in the MATLAB command window.

Error Handling

Print error information using the mexErrMsgIdAndTxt function.

Testing for Most-Derived Class

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

Upgrade MEX Files to Use 64-Bit API

How to upgrade your MEX files.

Upgrade MEX Files to Use Graphics Objects

MATLAB Version 8.4 (R2014b) changes the data type of handles to graphics objects from double to object.


Introducing MEX Files

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

Components of MEX File

Shows the components of a C/C++ source MEX file.

MATLAB API Libraries

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.

Data Flow in MEX Files

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

Handling Strings in C/C++

How MEX files handle C/C++ strings.

Memory Management

How to manage memory in MEX files.

Creating C++ MEX Files

C++ language issues to consider when creating and using MEX files.

Handling Large mxArrays

Use the 64-bit Matrix Library to build MEX files.

Handling Large File I/O

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

Was this topic helpful?