| MATLAB Application Program Interface | Search  Help Desk |
| mxSetField | Examples See Also |
Set a field value of a structure array, given a field name and an index
C Syntax
#include "matrix.h" void mxSetField(mxArray *array_ptr, int index, const char *field_name, mxArray *value);
Arguments
array_ptrmxArray. Call mxIsStruct to determine if array_ptr points to a structure mxArray.
index
mxArray has an index of 0, the second element has an index of 1, and the last element has an index of N-1, where N is the total number of elements in the structure mxArray. See mxCalcSingleSubscript for details on calculating an index.
field_name
mxGetFieldNameByNumber or mxGetFieldNumber to determine existing field names.
value
mxArray you are assigning.
Description
UsemxSetField to assign a value to the specified element of the specified field. In pseudo-C terminology, mxSetField performs the assignment
array_ptr[index].field_name = value;If there is already a value at the given position, the
value pointer you specified overwrites the old value pointer. However, mxSetField does not free the dynamic memory that the old value pointer pointed to. Consequently, you should free this old mxArray immediately before or after calling mxSetField.
Note: Inputs to a MEX-file are constant read-only mxArrays and should not be modified. Using mxSetCell* or mxSetField* to modify the cells or fields of an argument passed from MATLAB will cause unpredictable results.
Calling
mxSetField(pa, index, "field_name", new_value_pa);is equivalent to calling
field_num = mxGetFieldNumber(pa, "field_name"); mxSetFieldByNumber(pa, index, field_num, new_value_pa);
Example
Seemxcreatestructarray.c in the mx subdirectory of the examples directory.
See Also
mxCreateStructArray, mxCreateStructMatrix, mxGetField, mxGetFieldByNumber, mxGetFieldNameByNumber, mxGetFieldNumber, mxGetNumberOfFields, mxIsStruct, mxSetFieldByNumber