getfield
Reads a struct field, possibly nested or indexed, by name.
Category: Language Fundamentals > Data Types
Syntax
v = getfield(S, 'name')
Returns the value of field name.
v = getfield(S, 'a', 'b')
Follows nested fields.
v = getfield(S, 'a', {k})
Indexes into the field value.
Description
Each argument after S is one step of the path: text means a field, a cell means an index. getfield(S, 'a', 'b', {2}) is the same as S.a.b(2). With a name held in a variable, the dynamic field syntax S.(name) does the same job.
Input arguments
S- Structure.
name- Field name as text, or an index in a cell such as
{2}.
Output arguments
v- Value stored at the end of the path.
Example
Nested field with an index
s.a.b = [10 20 30];
getfield(s, 'a', 'b', {2})
getfield(s, 'a')
Output
ans = 20
ans = struct with fields:
b: [10 20 30]