Main Content

Tables of MEX Function Source Code Examples

The following tables contain lists of source code files for creating example MEX functions. Use these examples as a starting point for creating your own MEX functions. The tables contain the following information.

  • Example Name - a link that opens the source file in MATLAB® Editor for your convenience. You can use any code development editor to create source MEX files.

  • Example Subfolder - the subfolder of matlabroot/extern/examples containing the example. Use this subfolder name when copying the file to a writable folder.

  • Description - describes the example.

  • More Information - a link to a topic describing or using the example, or to the API function used in the example.

Getting Started

Use the mex command to build the examples. Make sure that you have a compiler installed that MATLAB supports. To verify the compiler selected for the source code language lang, type:

mex -setup lang

Copy the file to a writable folder on your path using the following command syntax. filename is the name of the example, and foldername is the subfolder name.


For example, to copy arrayProduct.c, type:


C, C++, and Fortran MEX Functions

To build an example MEX function in MATLAB or at your operating system prompt, use this command syntax. filename is the example name, and release-option specifies the API used by the example. For information about the MATLAB APIs, see Choosing MEX Applications.

mex -v -release-option filename
Example NameExample SubfolderDescriptionMore Information
arrayFillGetPr.c refbook

Fill mxArray using mxGetDoubles.

Fill mxArray in C MEX File
arrayFillSetData.c refbook

Fill mxArray with non-double values.

Fill mxArray in C MEX File
arrayFillSetPr.c refbook

Fill mxArray using mxSetDoubles to allocate memory dynamically.

Fill mxArray in C MEX File

Fill mxArray using mxSetComplexDoubles to allocate memory dynamically.

Fill mxArray in C MEX File

Multiply a scalar times 1xN matrix.

Create C Source MEX File

Same as arrayProduct.c, using the MATLAB Data API.

C++ MEX Functions
arraySize.c mex

Illustrate memory requirements of large mxArray.

Handling Large mxArrays in C MEX Files

Add two complex double arrays.


Pass complex data.

Handle Complex Data in C MEX File
refbookUse of Fortran %VAL. 
doubleelement.crefbookUse unsigned 16-bit integers.Handle 8-, 16-, 32-, and 64-Bit Data in C MEX File
explore.cmexIdentify data type of input variable.Work with mxArrays
findnz.c refbookUse N-dimensional arrays.Manipulate Multidimensional Numerical Arrays in C MEX Files
fulltosparse.F, loadsparse.F
refbookPopulate a sparse matrix.Handle Sparse Arrays in C MEX File
matsq.FrefbookPass matrices in Fortran. 
matsqint8.FrefbookPass non-double matrices in Fortran. 
mexRegister an exit function to close a data file.C++ File Handling Example

Call built-in MATLAB disp function.

mexcallmatlabwithtrap.c mexHow to capture error information. 
mexcpp.cppmexIllustrate some C++ language features in a MEX file built with the C Matrix API.C++ Class Example
mexevalstring.c mexUse mexEvalString to assign variables in MATLAB.mexEvalString
mexfunction.cmexHow to use mexFunction.mexFunction
mxgetproperty.cmexUse mxGetProperty and mxSetProperty to change the Color property of a graphic object.mxGetProperty and mxSetProperty
mexgetarray.cmexUse mexGetVariable and mexPutVariable to track counters in the MEX file and in the MATLAB global workspace.mexGetVariable and mexPutVariable
mexgetarray.cpp Same as mexgetarray.c, using matlab::engine::getVariable and matlab::engine::setVariable in the MATLAB Data API.Set and Get MATLAB Variables from MEX
mexHow to lock and unlock a MEX file.mexLock
mxcalcsinglesubscript.cmxDemonstrate MATLAB 1-based matrix indexing versus C 0-based indexing.mxCalcSingleSubscript
mxCreate 2-D cell array.Create 2-D Cell Array in C MEX File
mxcreatecharmatrixfromstr.cmxCreate 2-D character array.mxCreateCharMatrixFromStrings
mxcreatestructarray.cmxCreate MATLAB structure from C structure.mxCreateStructArray

Create an uninitialized mxArray, fill with local data, and return.


Read MATLAB eps value.


Read inf value.

mxgetnzmax.c mxDisplay number of nonzero elements in a sparse matrix and maximum number of nonzero elements it can store.mxGetNzmax
mxisclass.cmxCheck if array is member of specified class.mxIsClass
mxisfinite.cmxCheck for NaN and infinite values.mxIsFinite

Check if workspace variable is logical or global.

mxisscalar.cmxCheck if input variable is scalar.mxIsScalar

Allocate memory to copy a MATLAB char array to a C-style string.

mxReshape an array.mxSetDimensions
mxsetnzmax.cmxReallocate memory for sparse matrix and reset values of pr, pi, ir, and nzmax.mxSetNzmax
passstr.F refbookPass C character matrix from Fortran to MATLAB. 
phonebook.crefbookManipulate structures and cell arrays.Pass Structures and Cell Arrays in C MEX File
phonebook.cppcpp_mexSame as phonebook.c, using the MATLAB Data API.C++ MEX Functions

Copy MATLAB char array to and from C-style string.

Pass Strings in C MEX File
sincall.F, fill.F

Create mxArray and pass to MATLAB sin and plot functions.

refbookDemonstrate common workflow of MEX file.Pass Scalar Values in C MEX File
refbookPass multiple parameters.
yprimef.F, yprimefg.F
mexSolve simple three body orbit problem. 
yprime.cppcpp_mexSame as yprime.c, using the MATLAB Data API.C++ MEX Functions

C MEX Functions Calling Fortran Subroutines

The examples in the following table call a LAPACK or BLAS function using a C MEX function. These examples link to one or both of the Fortran libraries mwlapack and mwblas. To build the MEX function, follow the instructions in the topics listed in the More Information column.

Example Name Example Subfolder Description More Information
dotProductComplex.crefbookHandle Fortran complex return type for function called from a C MEX file.Handle Fortran Complex Return Type — dotProductComplex
matrixDivide.crefbookCall a LAPACK function.Preserve Input Values from Modification
matrixDivideComplex.crefbookCall a LAPACK function with complex numbers.Pass Complex Variables — matrixDivideComplex
matrixMultiply.crefbookCall a BLAS function.Pass Arguments to Fortran Functions from C/C++ Programs
utdu_slv.crefbookUse LAPACK for symmetric indefinite factorization.Symmetric Indefinite Factorization Using LAPACK — utdu_slv

See Also

Related Topics

External Websites