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/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 themex
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 languagelang
, type:
mex -setuplang
Copy the file to a writable folder on your path using the following command syntax.filename
is the name of the example, andfoldername
is 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')
To build an example MEX function in MATLAB or at your operating system prompt, use this command syntax.filename
is the example name, andrelease-option
specifies 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 |
Fill |
Fill mxArray in C MEX File |
一个rrayFillSetData.c |
refbook |
Fill |
Fill mxArray in C MEX File |
一个rrayFillSetPr.c |
refbook |
Fill |
Fill mxArray in C MEX File |
一个rrayFillSetPrComplex.c |
refbook |
Fill |
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 |
C++ MEX Functions |
一个rraySize.c |
mex |
Illustrate memory requirements of large |
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 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 |
UsemexEvalString to assign variables in MATLAB. |
mexEvalString |
mexfunction.c |
mex |
How to usemexFunction . |
mexFunction |
mxgetproperty.c |
mex |
UsemxGetProperty 一个ndmxSetProperty to change theColor property of a graphic object. |
mxGetProperty 一个ndmxSetProperty |
mexgetarray.c |
mex |
UsemexGetVariable 一个ndmexPutVariable to track counters in the MEX file and in the MATLAB global workspace. |
mexGetVariable 一个ndmexPutVariable |
mexgetarray.cpp |
Same asmexgetarray.c , usingmatlab::engine::getVariable 一个ndmatlab::engine::setVariable in 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 uninitialized |
mxCreateUninitNumericMatrix |
mxgeteps.c mxgetepsf.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 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 MATLAB |
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 MATLAB |
Pass Strings in C MEX File |
sincall.c sincall.F ,fill.F |
refbook |
Create |
|
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 |
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 |