C Matrix API
mxArray
type to represent MATLAB dataTo 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.
Note
Whenever possible, choose C++ over C applications.
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
.
Caution
The C Matrix API is not compatible with the MATLAB Data API for C++, which supports modern C++ features. You cannot use C Matrix API functions with functions in the MATLAB APIs described in Write C++ Functions Callable from MATLAB (MEX Files) or Call MATLAB from C++.
C Data Types
mxArray | C type for MATLAB array |
mwSize | C type for mxArray size values |
mwIndex | C type for mxArray index values |
mwSignedIndex | Signed integer C type for mxArray size values |
mxChar | Type for string array |
mxLogical | Type for logical array |
mxComplexity | Flag specifying whether array has imaginary components |
mxArray Attributes
mxIsNumeric | Determine whether mxArray is numeric |
mxIsComplex | Determine whether data is complex |
mxGetNumberOfDimensions | Number of dimensions in mxArray |
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 mxArray |
mxCalcSingleSubscript | Offset from first element to desired element |
mxGetM | Number of rows in mxArray |
mxSetM | Set number of rows in mxArray |
mxGetN | Number of columns in mxArray |
mxSetN | Set number of columns in mxArray |
mxIsEmpty | Determine whether mxArray is empty |
mxIsFromGlobalWS | Determine whether mxArray was copied from MATLAB global workspace |
Create, Query, and Access Data Types
Delete and Duplicate mxArray
mxDestroyArray | Free dynamic memory allocated by MXCREATE* functions |
mxDuplicateArray | Make deep copy of array |
Convert mxArray
Data Memory Management
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 |
Asserts
Constants
mxIsInf | Determine whether input is infinite |
mxIsFinite | Determine whether input is finite |
mxIsNaN | Determine whether input is NaN (Not-a-Number) |
Topics
- Pass Strings in C MEX File
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
andmxGetComplexDoubles
.