Tables of MEX Function Source Code Examples - MATLAB & Simulink (original) (raw)
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 compiler installed that MATLAB supports. To verify the compiler selected for the source code language_lang
_, type:
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')
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 mxArray usingmxGetDoubles. | Fill mxArray in C MEX File |
arrayFillSetData.c | refbook | Fill mxArray with non-double values. | Fill mxArray in C MEX File |
arrayFillSetPr.c | refbook | Fill mxArray usingmxSetDoubles to allocate memory dynamically. | Fill mxArray in C MEX File |
arrayFillSetComplexPr.c | refbook | Fill mxArray usingmxSetComplexDoubles to allocate memory dynamically. | Fill mxArray in C MEX File |
arrayProduct.c | mex | Multiply a scalar times 1xN matrix. | Create C Source MEX File arrayProduct.c |
arrayProduct.cpp | cpp_mex | Same as arrayProduct.c, using theMATLAB Data API for C++. | C++ MEX Functions |
arraySize.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 MATLABdisp function. | |
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 andmxSetProperty to change theColor property of a graphic object. | mxGetProperty and mxSetProperty |
mexgetarray.c | mex | Use mexGetVariable andmexPutVariable to track counters in the MEX file 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.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 uninitialized mxArray, fill with local data, and return. | mxCreateUninitNumericMatrix |
mxgeteps.c mxgetepsf.F | mx | Read MATLABeps value. | mxGetEps |
mxgetinf.c | mx | Read inf value. | 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 MATLABchar array 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, 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 for C++. | C++ MEX Functions |
revord.c revord.F | refbook | Copy MATLABchar array to and from C-style string. | Pass Strings in C MEX File |
sincall.c sincall.F, fill.F | refbook | Create mxArray and pass to MATLABsin and plot functions. | |
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 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.