how to get the specific member data from a struct array?
8 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
xiao
am 4 Nov. 2016
Kommentiert: Alexandra Harkai
am 7 Nov. 2016
for example,
b=struct('name','JPEG','age','18') ;
c=repmat(b,3,1) ;
d=c.name;
c.name
why is the result of "c.name" different from the result of "d=c.name"?
how to get the member data directly from a struct array or matrix? such as c.name?
and another question, how to create a struct array or matrix directly just like zeros(2,3), but struct(2,3) doesn't work, so how?
0 Kommentare
Akzeptierte Antwort
George
am 4 Nov. 2016
"When you access a field of a nonscalar structure, such as s.f, MATLAB® returns a comma-separated list. In this case, s.f is equivalent to s(1).f, s(2).f, s(3).f."
So your d = c.name is equivalent to
d = c(1).name, c(2).name, c(3).name.
Try
[d,e,f] = c.name
struct(2,3) doesn't work because it's trying to create a 1x1 struct array with field:2 and value:3. Field needs to be a char array.
Weitere Antworten (1)
Alexandra Harkai
am 4 Nov. 2016
Bearbeitet: Alexandra Harkai
am 4 Nov. 2016
What c is is what is called a nonscalar struct array. Then "You cannot assign the list to a single variable with the syntax v = s.f because the fields can contain different types of data." as per the Documentation, so variable d gets only c(1).name, and hence they will be different.
2 Kommentare
Alexandra Harkai
am 7 Nov. 2016
Please do not change your question once it has been answered, it is preferred to start new one.
Siehe auch
Kategorien
Mehr zu Structures finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!