Documentation

This is machine translation

Translated by Microsoft
Mouse over text to see original. Click the button below to return to the English verison of the page.

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 Example
arrayFillGetPr.crefbook

Fill mxArray using mxGetPr.

Fill mxArray
arrayFillSetData.crefbook

Fill mxArray with non-double values.

Fill mxArray
arrayFillSetPr.crefbook

Fill mxArray using mxSetPr to allocate memory dynamically.

Fill mxArray
arrayProduct.cmex

Multiply a scalar times 1xN matrix.

Create C Source MEX File
arraySize.cmex

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. 
dotProductComplex.crefbookHandle Fortran complex return type for function called from a C MEX file.Handling Complex Numbers in LAPACK and BLAS Functions
doubleelement.crefbookUse unsigned 16-bit integers.Handle 8-, 16-, and 32-Bit Data
explore.cmexIdentify data type of input variable.Work with mxArrays
findnz.crefbookUse N-dimensional arrays.Manipulate Multidimensional Numerical Arrays
fulltosparse.c
fulltosparse.F, loadsparse.F
refbookPopulate a sparse matrix.Handle Sparse Arrays
matrixDivide.crefbookCall a LAPACK function.Preserving Input Values from Modification
matrixDivideComplex.crefbookCall a LAPACK function with complex numbers.Handling Complex Numbers in LAPACK and BLAS Functions
matrixMultiply.crefbookCall a BLAS function.Passing Arguments to Fortran Functions from C/C++ Programs
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.cmexHow to capture error information. 
mexcpp.cppmexIllustrate some C++ language features in a MEX file.C++ Class in MEX Files
mexevalstring.cmexUse mexEvalString to assign variables in MATLAB.mexEvalString
mexfunction.cmexHow to use mexFunction.mexFunction
mexgetproperty.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
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.cmxRead inf value.mxGetInf
mxgetnzmax.cmxDisplay 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.cmxCheck 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.FrefbookPass C character matrix from Fortran to MATLAB. 
phonebook.crefbookManipulate structures and cell arrays.Pass Structures and Cell Arrays
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 workflow of MEX file.Pass Scalar Values
utdu_slv.crefbookUse LAPACK for symmetric indefinite factorization.Handling Complex Numbers in LAPACK and BLAS Functions
xtimesy.c
xtimesy.F
refbookPass multiple parameters. 
yprime.c
yprimef.F, yprimefg.F
mexSolve simple three body orbit problem. 

Was this topic helpful?