mxCreateStructArray (C and Fortran)

N-D structure array

C Syntax

#include "matrix.h"
mxArray *mxCreateStructArray(mwSize ndim, const mwSize *dims,
  int nfields, const char **fieldnames);

Fortran Syntax

mwPointer mxCreateStructArray(ndim, dims, nfields,
mwSize ndim
mwSize dims(ndim)
integer*4 nfields
character*(*) fieldnames(nfields)



Number of dimensions. If you set ndim to be less than 2, mxCreateStructArray creates a two-dimensional mxArray.


Dimensions array. Each element in the dimensions array contains the size of the array in that dimension. For example, in C, setting dims[0] to 5 and dims[1] to 7 establishes a 5-by-7 mxArray. In Fortran, setting dims(1) to 5 and dims(2) to 7 establishes a 5-by-7 mxArray. Typically, the dims array should have ndim elements.


Number of fields in each element


List of field names

Each structure field name must begin with a letter and is case-sensitive. The rest of the name can contain letters, numerals, and underscore characters. Use the namelengthmax function to determine the maximum length of a field name.


Pointer to the created mxArray, if successful. If unsuccessful in a standalone (non-MEX-file) application, returns NULL in C (0 in Fortran). If unsuccessful in a MEX-file, the MEX-file terminates and returns control to the MATLAB® prompt. The function is unsuccessful when there is not enough free heap space to create the mxArray.


Call mxCreateStructArray to create an unpopulated structure mxArray. Each element of a structure mxArray contains the same number of fields (specified in nfields). Each field has a name; the list of names is specified in fieldnames. A MATLAB structure mxArray is conceptually identical to an array of structs in the C language.

Each field holds one mxArray pointer. mxCreateStructArray initializes each field to NULL in C (0 in Fortran). Call mxSetField or mxSetFieldByNumber to place a non-NULL mxArray pointer in a field.

When you finish using the returned structure mxArray, call mxDestroyArray to reclaim its space.

Any trailing singleton dimensions specified in the dims argument are automatically removed from the resulting array. For example, if ndim equals 5 and dims equals [4 1 7 1 1], the resulting array is given the dimensions 4-by-1-by-7.


See the following examples in matlabroot/extern/examples/mx.

Was this topic helpful?