| MATLAB Application Program Interface | Search  Help Desk |
| mxSetFieldByNumber | Examples See Also |
Set a field value in a structure array, given a field number and an index
C Syntax
#include "matrix.h" void mxSetFieldByNumber(mxArray *array_ptr, int index, int field_number, 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_number
field_number of 0, the second field has a field_number of 1, and so on. The last field has a field_number of N-1, where N is the number of fields.
value
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.
Description
UsemxSetFieldByNumber to assign a value to the specified element of the specified field. mxSetFieldByNumber is almost identical to mxSetField; however, the former takes a field number as its third argument and the latter takes a field name as its third argument.
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);
Examples
Seemxcreatestructarray.c in the mx subdirectory of the examples directory. For an additional example, see phonebook.c in the refbook subdirectory of the examples directory.
See Also
mxCreateStructArray, mxCreateStructMatrix, mxGetField, mxGetFieldByNumber, mxGetFieldNameByNumber, mxGetFieldNumber, mxGetNumberOfFields, mxIsStruct, mxSetField