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 ofmatlabroot/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.

开始

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

mex -setuplang

Copy the file to a writable folder on your path using the following command syntax.filenameis the name of the example, andfoldernameis the subfolder name.

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

For example, to copy一个rrayProduct.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.filenameis the example name, andrelease-optionspecifies the API used by the example. For information about the MATLAB APIs, seeChoosing MEX Applications.

mex -v -release-optionfilename
Example Name Example Subfolder Description More Information
arrayFillGetPr.c refbook

FillmxArrayusingmxGetDoubles.

Fill mxArray in C MEX File
一个rrayFillSetData.c refbook

FillmxArraywith non-double values.

Fill mxArray in C MEX File
一个rrayFillSetPr.c refbook

FillmxArrayusingmxSetDoublesto allocate memory dynamically.

Fill mxArray in C MEX File
一个rrayFillSetPrComplex.c refbook

FillmxArrayusingmxSetComplexDoublesto allocate memory dynamically.

Fill mxArray in C MEX File
一个rrayProduct.c mex

Multiply a scalar times 1xN matrix.

Create C Source MEX File
一个rrayProduct.cpp cpp_mex

Same as一个rrayProduct.c, using theMATLAB Data API.

C++ MEX Functions
一个rraySize.c mex

Illustrate memory requirements of largemxArray.

Handling Large mxArrays in C MEX Files
complexAdd.F refbook

Add two complex double arrays.

convec.c
convec.F
refbook

Pass complex data.

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

Call built-in MATLABdispfunction.

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 UsemexEvalStringto assign variables in MATLAB. mexEvalString
mexfunction.c mex How to usemexFunction. mexFunction
mxgetproperty.c mex UsemxGetProperty一个ndmxSetPropertyto change theColorproperty of a graphic object. mxGetProperty一个ndmxSetProperty
mexgetarray.c mex UsemexGetVariable一个ndmexPutVariableto track counters in the MEX file and in the MATLAB global workspace. mexGetVariable一个ndmexPutVariable
mexgetarray.cpp Same asmexgetarray.c, usingmatlab::engine::getVariable一个ndmatlab::engine::setVariablein theMATLAB Data API. Set and Get MATLAB Variables from MEX
mexlock.c
mexlockf.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.c
mxcreatecellmatrixf.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 uninitializedmxArray, fill with local data, and return.

mxCreateUninitNumericMatrix
mxgeteps.c
mxgetepsf.F
mx

Read MATLABepsvalue.

mxGetEps
mxgetinf.c mx

Readinfvalue.

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 forNaN一个nd 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 MATLABchar一个rray to a C-style string.

mxMalloc
mxsetdimensions.c
mxsetdimensionsf.F
mx Reshape an array. mxSetDimensions
mxsetnzmax.c mx Reallocate memory for sparse matrix and reset values ofpr,pi,ir, andnzmax. 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 asphonebook.c, using theMATLAB Data API. C++ MEX Functions
revord.c
revord.F
refbook

Copy MATLABchar一个rray to and from C-style string.

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

CreatemxArray一个nd pass to MATLABsin一个ndplotfunctions.

timestwo.c
timestwo.F
refbook Demonstrate common workflow of MEX file. Pass Scalar Values in C MEX File
xtimesy.c
xtimesy.F
refbook Pass multiple parameters.
yprime.c
yprimef.F,yprimefg.F
mex Solve simple three body orbit problem.
yprime.cpp cpp_mex Same asyprime.c, using theMATLAB 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 librariesmwlapack一个ndmwblas. 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

See Also

Related Topics

External Websites