MATLAB Function Reference
setfield

Set field of structure array

 Note    `setfield` is obsolete and will be removed in a future release. Please use dynamic field names instead.

Syntax

• ```s = setfield(s,'field',v)
s = setfield(s,{i,j},'field',{k},v)
```

Description

```s = setfield(s,'field',v), ``` where `s` is a 1-by-1 structure, sets the contents of the specified field to the value `v`. This is equivalent to the syntax
`s.field = v`.

```s = setfield(s,{i,j},'field',{k},v) ``` sets the contents of the specified field to the value `v`. This is equivalent to the syntax `s(i,j).field(k) = v`. All subscripts must be passed as cell arrays--that is, they must be enclosed in curly braces (similar to`{i,j}` and `{k}` above). Pass field references as strings.

Examples

Given the structure

• ```mystr(1,1).name = 'alice';
mystr(1,1).ID = 0;
mystr(2,1).name = 'gertrude';
mystr(2,1).ID = 1;
```

You can change the `name` field of `mystr(2,1)` using

• ````mystr = setfield(mystr,{2,1},'name','ted')`;
mystr(2,1).name

ans =

ted
```

The following example sets fields of a structure using `setfield` with variable and quoted field names and additional subscripting arguments.

• ```class = 5;   student = 'John_Doe';

```

You can check the outcome using the standard structure syntax.

• ```grades(class).John_Doe.Math(10,21:30)

ans =

85    89    76    93    85    91    68    84    95    73
```

`fieldnames`, `isfield`, `orderfields`, `rmfield`