Tables of MEX Function Source Code Examples
Note
It is recommended to use clibPublishInterfaceWorkflow to write code in MATLAB® R2023a or later to directly call functions in C/C++ libraries.
Alternatively, to write MEX functions using modern C++ features and the MATLAB Data API for C++, see Write C++ Functions Callable from MATLAB (MEX Files).
The following tables contain lists of source code files for creating example MEX
functions using mxArray in the C Matrix API. 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/examplescontaining 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(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 Name | Example Subfolder | Description | More Information |
|---|---|---|---|
arrayFillGetPr.c
| refbook
| Fill | Fill mxArray in C MEX Function |
arrayFillSetData.c
| refbook | Fill | Fill mxArray in C MEX Function |
arrayFillSetPr.c
| refbook | Fill | Fill mxArray in C MEX Function |
arrayFillSetComplexPr.c | refbook | Fill | Fill mxArray in C MEX Function |
arrayProduct.c | mex | Multiply a scalar times 1xN matrix. | Create C MEX Function arrayProduct |
arrayProduct.cpp | cpp_mex | Same as | C++ MEX Functions |
arraySize.c
| mex | Illustrate memory requirements of large
| Handling Large mxArrays in C MEX Functions |
complexAdd.F | refbook | Add two complex double arrays. | |
convec.cconvec.F
| refbook | Pass complex data. | Handle Complex Data in C MEX Function |
dblmat.Fcompute.F
| refbook | Use of Fortran %VAL. | |
doubleelement.c | refbook | Use unsigned 16-bit integers. | Handle 8-, 16-, 32-, and 64-Bit Data in C MEX Function |
explore.c | mex | Identify data type of input variable. | Work with mxArrays |
findnz.c
| refbook | Use N-dimensional arrays. | Manipulate Multidimensional Numerical Arrays in C MEX Functions |
fulltosparseIC.cfulltosparse.cfulltosparse.F, loadsparse.F
| refbook | Populate a sparse matrix. | Handle Sparse Arrays in C MEX Function |
matsq.F | refbook | Pass matrices in Fortran. | |
matsqint8.F | refbook | Pass non-double matrices in Fortran. | |
mexatexit.cmexatexit.cpp | mex | Register an exit function to close a data file. | C++ File Handling Example |
mexcallmatlab.c | mex | Call built-in MATLAB
| |
mexcallmatlabwithtrap.c
| mex | How to capture error information. | |
mexcpp.cpp | mex | Illustrate C++ language features in a MEX function built with the C Matrix API. | C++ Class Example |
mexevalstring.c
| mex | Use mexEvalString to assign variables in
MATLAB. | mexEvalString |
mexfunction.c | mex | How to use mexFunction. | mexFunction |
mxgetproperty.c | mex | Use mxGetProperty and
mxSetProperty to change the
Color property of a graphic
object. | mxGetProperty
and mxSetProperty |
mexgetarray.c | mex | Use mexGetVariable and
mexPutVariable to track counters in the
MEX function 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.cmexlockf.F | mex | How to lock and unlock a MEX function. | mexLock |
mxcalcsinglesubscript.c | mx | Demonstrate MATLAB 1-based matrix indexing versus C 0-based indexing. | mxCalcSingleSubscript |
mxcreatecellmatrix.cmxcreatecellmatrixf.F | mx | Create 2-D cell array. | Create 2-D Cell Array in C MEX Function |
mxcreatecharmatrixfromstr.c | mx | Create 2-D character array. | mxCreateCharMatrixFromStrings |
mxcreatestructarray.c | mx | Create MATLAB structure from C structure. | mxCreateStructArray |
mxcreateuninitnumericmatrix.c | mx | Create an uninitialized | mxCreateUninitNumericMatrix |
mxgeteps.cmxgetepsf.F
| mx | Read MATLAB
| mxGetEps |
mxgetinf.c | mx | Read | mxGetInf |
mxgetnzmax.c
| mx | Display number of nonzero elements in a sparse matrix and maximum number of nonzero elements it can store. | mxGetNzmax |
mxisclass.c | mx | Check if array is member of specified class. | mxIsClass |
mxisfinite.c | mx | Check for NaN and infinite values. | mxIsFinite |
mxislogical.c | mx | Check if workspace variable is logical or global. | mxIsLogical |
mxisscalar.c | mx | Check if input variable is scalar. | mxIsScalar |
mxmalloc.c | mx | Allocate memory to copy a MATLAB
| mxMalloc |
mxsetdimensions.cmxsetdimensionsf.F
| mx | Reshape an array. | mxSetDimensions |
mxsetnzmax.c | mx | Reallocate memory for sparse matrix and reset values of
pr, pi,
ir, and nzmax. | mxSetNzmax |
passstr.F
| refbook | Pass C character matrix from Fortran to MATLAB. | |
phonebook.c | refbook | Manipulate structures and cell arrays. | Pass Structures and Cell Arrays in C MEX Function |
phonebook.cpp | cpp_mex | Same as phonebook.c, using the MATLAB Data API for C++. | C++ MEX Functions |
revord.crevord.F | refbook | Copy MATLAB
| Pass Strings in C MEX Function |
sincall.csincall.F, fill.F | refbook | Create | |
timestwo.ctimestwo.F | refbook | Demonstrate common workflow of MEX function. | Pass Scalar Values in C MEX Function |
xtimesy.cxtimesy.F | refbook | Pass multiple parameters. | |
yprime.cyprimef.F, yprimefg.F | mex | Solve simple three body orbit problem. | |
yprime.cpp | cpp_mex | Same 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.cdotProductComplexIC.FdotProductComplex.c | refbook | Handle Fortran complex return type for function called from a C
or Fortran MEX
function.dotProductComplexIC.c
and dotProductComplexIC.F use the interleaved
complex API. dotProductComplex.c uses the
separate complex API. | Handle Fortran Complex Return Type — dotProductComplex |
matrixDivide.c | refbook | Call a LAPACK function. | Preserve Input Values from Modification |
matrixDivideComplex.c | refbook | Call a LAPACK function with complex numbers. | Pass Complex Variables — matrixDivideComplex |
matrixMultiply.c | refbook | Call a BLAS function. | Pass Arguments to Fortran Functions from C/C++ Programs |
utdu_slv.c | refbook | Use LAPACK for symmetric indefinite factorization. | Symmetric Indefinite Factorization Using LAPACK — utdu_slv |
See Also
clibPublishInterfaceWorkflow | mex