| MATLAB Function Reference | Search  Help Desk |
| setfield | Examples See Also |
s = setfield(s,'field',v)
s = setfield(s,{i,j},'field',{k},v)
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.
Given the structure:
mystr(1,1).name = 'alice'; mystr(1,1).ID = 0; mystr(2,1).name = 'gertrude'; mystr(2,1).ID = 1Then the command
mystr = setfield(mystr,{2,1},'name','ted') yields
mystr =
2x1 struct array with fields:
name
ID
fields Field names of a structure
getfield Get field of structure array