access struct data (matlab) -
a= struct('a1',{1,2,3},'a2',{4,5,6})
how can iget value of 1;
i try use a.a1{1} return errors
>> a.a1{1} ??? field reference multiple structure elements followed more reference blocks error.
how can access 1? thanks.
edit a = struct{'a1',[1 2 3],'a2',[4 5 6]}
how can access 1. use a(1).a1
1 2 3
you have instead:
a(1).a1
the reason why because code use create structure creates 3-element structure array first array element contains a1: 1
, a2: 4
, second array element contains a1: 2
, a2: 5
, , third array element contains a1: 3
, a2: 6
.
when use curly braces {}
in call struct did, matlab assumes wanting create structure array in distribute contents of cells across structure array elements. if instead want create single 1-by-1 structure element fields contain cell arrays, have add additional set of curly braces enclosing cell arrays, so:
a = struct('a1',{{1,2,3}},'a2',{{4,5,6}});
then original a.a1{1}
work.
edit:
if create structure using numeric arrays instead of cell arrays, so:
a = struct('a1',[1 2 3],'a2',[4 5 6]);
then can access value of 1 follows:
a.a1(1)
for further information working structures in matlab, check out this documentation page.
Comments
Post a Comment