Documentation

This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

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 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, where 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, where filename is the example name and release-option specifies the API used by the example. For information about the MATLAB APIs, see MEX Applications.

mex -v -release-option filename
  • For C and C++ MEX files, type:

    mex -v -R2018a filename
  • For Fortran MEX files, type:

    mex -v -R2017b filename

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

    mex -v -R2017b file1.F file2.F
Example Name Example Subfolder Description More Information
arrayFillGetPr.c refbook

Fill mxArray using mxGetPr.

Fill mxArray
arrayFillSetData.c refbook

Fill mxArray with non-double values.

Fill mxArray
arrayFillSetPr.c refbook

Fill mxArray using mxSetPr to allocate memory dynamically.

Fill mxArray
arrayProduct.cmex

Multiply a scalar times 1xN matrix.

Create C Source MEX File
arrayProduct.cppcpp_mex

Same as arrayProduct.c, using the MATLAB Data API.

C++ MEX Functions
arraySize.c mex

Illustrate memory requirements of large mxArray.

Handling Large mxArrays
convec.c
convec.F
refbook

Pass complex data.

Handle Complex Data
dblmat.F
compute.F
refbookUse of Fortran %VAL. 
doubleelement.crefbookUse unsigned 16-bit integers.Handle 8-, 16-, 32-, and 64-Bit Data
explore.cmexIdentify data type of input variable.Work with mxArrays
findnz.c refbookUse N-dimensional arrays.Manipulate Multidimensional Numerical Arrays
fulltosparse.c
fulltosparse.F, loadsparse.F
refbookPopulate a sparse matrix.Handle Sparse Arrays
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.Handle Files with C++
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.Use C++ Class in C MEX File
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 matlab::engine::getVariable and matlab::engine::setVariable in the MATLAB Data API.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
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 Files
phonebook.cppcpp_mexSame as phonebook.c, using the MATLAB Data API.C++ MEX Functions
revord.c
revord.F
refbook

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

Pass Strings
sincall.c
sincall.F, fill.F
refbook

Create mxArray and pass to MATLAB sin and plot functions.

 
timestwo.c
timestwo.F
refbookDemonstrate common work flow of MEX file.Pass Scalar Values
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.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 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.crefbookHandle Fortran complex return type for function called from a C MEX file.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

External Websites

Was this topic helpful?