MATLAB Application Program Interface
  Go to function:
    Search    Help Desk 
mxSetField    Examples   See Also

Set a field value of a structure array, given a field name and an index

C Syntax

Arguments

array_ptr
  Pointer to a structure mxArray. Call mxIsStruct to determine if array_ptr points to a structure mxArray.

index
  The desired element. The first element of an 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
  The name of the field whose value you are assigning. Call mxGetFieldNameByNumber or mxGetFieldNumber to determine existing field_names.

value
  Pointer to the mxArray you are assigning.

Description

Use mxSetField to assign a value to the specified element of the specified field. In pseudo-C terminology, mxSetField performs the assignment

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 typically 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.

Examples

Consider a function that expects to receive a structure mxArray containing a speed field. The speed field contains a numeric scalar. The function calls mxSetField to change the value in the speed field to a string mxArray.

For an additional example, see mxsetfield.c in the mx subdirectory of the examples directory.

See Also

mxCreateStructArray, mxCreateStructMatrix, mxGetField, mxGetFieldByNumber, mxGetFieldNameByNumber, mxGetFieldNumber, mxGetNumberOfFields, mxIsStruct, mxSetFieldByNumber



[ Previous | Help Desk | Next ]