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.

copyfile(fullfile(matlabroot,'extern','examples','foldername','filename'),'.','f')

For example, to copy arrayProduct.c, type:

copyfile(fullfile(matlabroot,'extern','examples','mex','arrayProduct.c'),'.','f')

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
arrayFillSetComplexPr.crefbook

Fill mxArray using mxSetComplexDoubles to allocate memory dynamically.

Fill mxArray in C MEX File
arrayProduct.cmex

Multiply a scalar times 1xN matrix.

Create C Source MEX File arrayProduct.c
arrayProduct.cppcpp_mex

Same as arrayProduct.c, using the MATLAB Data API for C++.

C++ MEX Functions
arraySize.c mex

Illustrate memory requirements of large mxArray.

Handling Large mxArrays in C MEX Files
complexAdd.Frefbook

Add two complex double arrays.

 
convec.c
convec.F
refbook

Pass complex data.

Handle Complex Data in C MEX File
dblmat.F
compute.F
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
fulltosparseIC.c
fulltosparse.c
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. 
mexatexit.c
mexatexit.cpp
mexRegister an exit function to close a data file.C++ File Handling Example
mexcallmatlab.cmex

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 getVariable and setVariable in the MATLAB Data API for C++.Set and Get MATLAB Variables from MEX
mexlock.c
mexlockf.F
mexHow to lock and unlock a MEX file.mexLock
mxcalcsinglesubscript.cmxDemonstrate MATLAB 1-based matrix indexing versus C 0-based indexing.mxCalcSingleSubscript
mxcreatecellmatrix.c
mxcreatecellmatrixf.F
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
mxcreateuninitnumericmatrix.cmx

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

mxCreateUninitNumericMatrix
mxgeteps.c
mxgetepsf.F
mx

Read MATLAB eps value.

mxGetEps
mxgetinf.cmx

Read inf value.

mxGetInf
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
mxislogical.cmx

Check if workspace variable is logical or global.

mxIsLogical
mxisscalar.cmxCheck if input variable is scalar.mxIsScalar
mxmalloc.cmx

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

mxMalloc
mxsetdimensions.c
mxsetdimensionsf.F
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 for C++.C++ MEX Functions
revord.c
revord.F
refbook

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

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

Create mxArray and pass to MATLAB sin and plot functions.

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

MEX Functions Calling Fortran Subroutines

The examples in this table call a LAPACK or BLAS function from a MEX function. The 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
dotProductComplexIC.c
dotProductComplexIC.F
dotProductComplex.c
refbookHandle Fortran complex return type for function called from a C or Fortran MEX file.
dotProductComplexIC.c and dotProductComplexIC.F use the interleaved complex API. dotProductComplex.c uses the separate complex API.
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