Documentation

Table of MEX File Source Code Files

Source code for the MEX examples shown in the following table is in subfolders of matlabroot/extern/examples. Make sure that you have a MATLAB®-supported compiler installed.

To build a code example, first copy the file to a writable folder on your path:

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

where filename is the name of the example, for example arrayProduct.c, and foldername is the subfolder name, for example mex.

You can create and compile MEX files in MATLAB or at your operating system prompt. At either prompt, type:

mex -v filename

For examples listing multiple Fortran sources files, both files are required to build the MEX file.

mex -v file1.F file2.F

You can modify the source code for the examples. For convenience, the example instructions open the files in MATLAB Editor, but you can use any code development editor.

Example Name Example Subfolder Description
arrayFillGetPr.crefbookFill mxArray using mxGetPr.
arrayFillSetData.crefbookFill mxArray with non-double values.
arrayFillSetPr.crefbookFill mxArray using mxSetPr to allocate memory dynamically.
arrayProduct.cmexMultiply a scalar times 1xN matrix.
arraySize.cmexIllustrate memory requirements of large mxArray.
convec.c
convec.F
refbookPass complex data.
dblmat.F
compute.F
refbookUse of Fortran %VAL.
dotProductComplex.crefbookHandle Fortran complex return type for function called from a C MEX file.
doubleelement.crefbookUse unsigned 16-bit integers.
explore.cmexIdentify data type of input variable.
findnz.crefbookUse N-dimensional arrays.
fulltosparse.c
fulltosparse.F, loadsparse.F
refbookPopulate a sparse matrix.
matrixDivide.crefbookCall a LAPACK function.
matrixDivideComplex.crefbookCall a LAPACK function with complex numbers.
matrixMultiply.crefbookCall a BLAS function.
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.
mexcallmatlab.cmex

Call built-in MATLAB disp function.

mexcallmatlabwithtrap.cmexHow to capture error information.
mexcpp.cppmexIllustrate some C++ language features in a MEX file.
mexevalstring.cmexUse mexEvalString to assign variables in MATLAB.
mexfunction.cmexHow to use mexfunction.
mexgetproperty.cmexUse mxGetProperty and mxSetProperty to change the Color property of a graphic object.
mexgetarray.cmexUse mexGetVariable and mexPutVariable to track counters in the MEX file and in the MATLAB global workspace.
mexlock.c
mexlockf.F
mexHow to lock and unlock a MEX file.
mxcalcsinglesubscript.cmxDemonstrate MATLAB 1-based matrix indexing versus C 0-based indexing.
mxcreatecellmatrix.c
mxcreatecellmatrixf.F
mxCreate 2-D cell array.
mxcreatecharmatrixfromstr.cmxCreate 2-D string array.
mxcreatestructarray.cmxCreate MATLAB structure from C structure.
mxgeteps.c
mxgetepsf.F
mx

Read MATLAB eps value.

mxgetinf.cmxRead inf value.
mxgetnzmax.cmxDisplay number of nonzero elements in a sparse matrix and maximum number of nonzero elements it can store.
mxisclass.cmxCheck if array is member of specified class.
mxisfinite.cmxCheck for NaN and infinite values.
mxislogical.cmxCheck if workspace variable is logical or global.
mxmalloc.cmxAllocate memory to copy a MATLAB string to a C string.
mxsetdimensions.c
mxsetdimensionsf.F
mxReshape an array.
mxsetnzmax.cmxReallocate memory for sparse matrix and reset values of pr, pi, ir, and nzmax.
passstr.FrefbookPass C character matrix from Fortran to MATLAB.
phonebook.crefbookManipulate structures and cell arrays.
revord.c
revord.F
refbookCopy MATLAB string data to and from C-style string.
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.
utdu_slv.crefbookUse LAPACK for symmetric indefinite factorization.
xtimesy.c
xtimesy.F
refbookPass multiple parameters.
yprime.c
yprimef.F, yprimefg.F
mexSolve simple three body orbit problem.

Was this topic helpful?