MATLAB Function Reference | Search  Help Desk |
getfield | Examples See Also |
f = getfield(s,'field') f = getfield(s,{i,j},'field',{k})
f = getfield(s,'field'),
where s
is a 1-by-1 structure, returns the contents of the specified field. This is equivalent to the syntax f = s.field
.
f = getfield(s,{i,j},'field',{k})
returns the contents of the specified field. This is equivalent to the syntax f = s(i,j).field(k)
. 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
f = getfield(mystr,{2,1},'name')
yields
f = gertrudeTo list the contents of all name (or other) fields, embed
getfield
in a loop:
for i = 1:2 name{i} = getfield(mystr,{i,1},'name'); end name name = 'alice' 'gertrude'
fields
Field names of a structure
setfield
Set field of structure array