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.
Use the mex command to build the examples.
Make sure that you have a MATLAB-supported compiler installed. 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')
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 File |
arrayFillSetData.c
| refbook | Fill | Fill mxArray in C MEX File |
arrayFillSetPr.c
| refbook | Fill | Fill mxArray in C MEX File |
arrayFillSetPrComplex.c | refbook | Fill | Fill mxArray in C MEX File |
arrayProduct.c | mex | Multiply a scalar times 1xN matrix. | Create C Source MEX File |
arrayProduct.cpp | cpp_mex | Same as | C++ MEX Functions |
arraySize.c
| mex | Illustrate memory requirements of large
| Handling Large mxArrays in C MEX Files |
complexAdd.F | refbook | Add two complex double arrays. | |
convec.cconvec.F
| refbook | Pass complex data. | Handle Complex Data in C MEX File |
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 File |
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 Files |
fulltosparseIC.cfulltosparse.cfulltosparse.F, loadsparse.F
| refbook | Populate a sparse matrix. | Handle Sparse Arrays in C MEX File |
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 some C++ language features in a MEX file 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 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 | |
mexlock.cmexlockf.F | mex | How to lock and unlock a MEX file. | 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 File |
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 File |
phonebook.cpp | cpp_mex | Same as phonebook.c, using the MATLAB Data API. | C++ MEX Functions |
revord.crevord.F | refbook | Copy MATLAB
| Pass Strings in C MEX File |
sincall.csincall.F, fill.F | refbook | Create | |
timestwo.ctimestwo.F | refbook | Demonstrate common workflow of MEX file. | Pass Scalar Values in C MEX File |
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. | C++ MEX Functions |
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.c | refbook | Handle Fortran complex return type for function called from a C MEX file. | 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 |