mxArray
data structureTo write C programs that work with MATLAB R2017b or earlier, use functions in the C Matrix API. These functions
work with the mxArray
data structure and are
used to build C MEX functions or C engine applications.
MathWorks recommends that whenever possible, choose C++ over C applications. For more information, see Calling MATLAB from C++ or C++ Libraries to directly call C++ library functionality from MATLAB.
There are two versions of the C Matrix API.
The interleaved complex API contains the C Matrix API functionality as of MATLAB R2018a. For more information, see MATLAB Support for Interleaved Complex API in MEX Functions.
The separate complex API contains the C Matrix API functionality as of MATLAB R2017b.
For examples using these library functions, see:
See individual functions for example information. For example, see
mxIsChar
.
The C Matrix API is not compatible with the MATLAB Data API, which supports modern C++ features. You cannot use C Matrix API functions with functions in the C++ MEX Applications or MATLAB Engine API for C++.
mxArray | C type for MATLAB array |
mwSize | Type for size values |
mwIndex | Type for index values |
mwSignedIndex | Signed integer type for size values |
mxChar | Type for string array |
mxLogical | Type for logical array |
mxComplexity | Flag specifying whether array has imaginary components |
mxIsNumeric | Determine whether array is numeric |
mxIsComplex | Determine whether data is complex |
mxGetNumberOfDimensions | Number of dimensions in array |
mxGetElementSize | Number of bytes required to store each data element |
mxGetDimensions | Pointer to dimensions array |
mxSetDimensions | Modify number of dimensions and size of each dimension |
mxGetNumberOfElements | Number of elements in numeric array |
mxCalcSingleSubscript | Offset from first element to desired element |
mxGetM | Number of rows in array |
mxSetM | Set number of rows in array |
mxGetN | Number of columns in array |
mxSetN | Set number of columns in array |
mxIsEmpty | Determine whether array is empty |
mxIsFromGlobalWS | Determine whether array was copied from MATLAB global workspace |
mxDestroyArray | Free dynamic memory allocated by MXCREATE* functions |
mxDuplicateArray | Make deep copy of array |
mxCalloc | Allocate dynamic memory for array, initialized to 0, using MATLAB memory manager |
mxMalloc | Allocate uninitialized dynamic memory using MATLAB memory manager |
mxRealloc | Reallocate dynamic memory using MATLAB memory manager |
mxFree | Free dynamic memory allocated by mxCalloc, mxMalloc, mxRealloc, mxArrayToString, or mxArrayToUTF8String functions |
mxIsInf | Determine whether input is infinite |
mxIsFinite | Determine whether input is finite |
mxIsNaN | Determine whether input is NaN (Not-a-Number) |
This example shows how to pass strings to a MEX function built with the C Matrix API.
Pass Scalar Values in C MEX File
This example shows how to write a MEX file that passes scalar values.
Handle Sparse Arrays in C MEX File
This example shows how to populate a sparse matrix using the C Matrix API.
Handle 8-, 16-, 32-, and 64-Bit Data in C MEX File
This example shows how to use unsigned 16-bit integers in a MEX file using the C Matrix API.
Pass Structures and Cell Arrays in C MEX File
This example shows how to pass structures and cell arrays to MEX files built with the C Matrix API.
Manipulate Multidimensional Numerical Arrays in C MEX Files
You can manipulate multidimensional numerical arrays by using typed data access
functions like mxGetDoubles
and
mxGetComplexDoubles
.