setfield
Sets the value of a structure field.
Category: Language Fundamentals > Data Types
Syntax
s = setfield(s, field, value)
Sets s.field to value.
s = setfield(s, field1, field2, ..., value)
Sets a nested field, as in s.field1.field2.
Description
setfield is the function form of s.field = value. Dot notation is shorter, but setfield is handy when the field names are computed or when it is used in an anonymous function. Dynamic field names also work in dot notation, as in s.(name) = value.
Input arguments
s- Structure.
field- Field name as text; the field is created if absent.
value- Any value.
Output arguments
s- The updated structure.
Example
s = struct('b', 2);
s = setfield(s, 'c', 3)
s = setfield(s, 'p', 'q', 5);
s.p.q
Output
s = struct with fields:
b: 2
c: 3
ans = 5