Note: This page has been translated by MathWorks. Click here to see

To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

Assign value to structure array field

`S = setfield(S,field,value)`

`S = setfield(S,level1,...,levelN,field,value)`

`S = setfield(S,structIndices,field,value)`

`S = setfield(S,structIndices,level1,...,levelN,field,value)`

`S = setfield(S,structIndices,level1,level1Indices,...,levelN,levelNIndices,field,value)`

`S = setfield(___,field,fieldIndices,value)`

`S = setfield(`

assigns a value to the specified field of a `S`

,`level1,...,levelN`

,`field`

,`value`

)*nested structure*. In a
nested structure, the value of a field can be another structure. For example, ```
S =
setfield(S,'a','b','c',1)
```

makes the assignment `S.a.b.c = 1`

,
where the fields `S.a`

and `S.a.b`

are also structures
at the first and second levels of nesting.

`S = setfield(`

specifies an element of `S`

,`structIndices`

,`field`

,`value`

)`S`

using the cell array
`structIndices`

when `S`

is a structure array. For
example, `S = setfield(S,{3},'a',1)`

makes the assignment ```
S(3).a
= 1
```

, where `S(3)`

is the third element of
`S`

.

`S = setfield(`

specifies a field of a nested structure when `S`

,`structIndices`

,`level1,...,levelN`

,`field`

,`value`

)`S`

is a structure array.
For example, `S = setfield(S,{3,4},'a','b','c',1)`

makes the assignment
`S(3,4).a.b.c = 1`

.

`S = setfield(`

specifies elements of `S`

,`structIndices`

,level1,level1Indices,...,levelN,`levelNIndices`

,`field`

,`value`

)`S`

, `levelN`

, and structure
arrays nested at any level in between, when any or all of the structures can be
nonscalar.

`S = setfield(___,`

assigns the elements of `field`

,`fieldIndices`

,`value`

)`value`

to elements of the array stored in
`field`

and specified by `fieldIndices`

. You can use
this syntax with the nested levels and structure array indices specified in any of the
previous syntaxes.

Usually, it is simpler to assign values to fields using dot syntax rather than the

`setfield`

function. Use`setfield`

to:Avoid creating unnecessary temporary variables.

Assign values to fields of nested structures.

`fieldnames`

| `getfield`

| `isfield`

| `orderfields`

| `rmfield`

| `struct`