MATLAB Answers

how to get the specific member data from a struct array?

8 views (last 30 days)
xiao on 4 Nov 2016
for example,
b=struct('name','JPEG','age','18') ;
c=repmat(b,3,1) ;;
why is the result of "" different from the result of ""?
how to get the member data directly from a struct array or matrix? such as
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?


Sign in to comment.

Accepted Answer

George on 4 Nov 2016
From the doc on struct
"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 = is equivalent to
d = c(1).name, c(2).name, c(3).name.
[d,e,f] =
to see the difference. See here for more info on how to access data in a struct.
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.

  1 Comment

xiao on 5 Nov 2016
thank you! inspired by you, i try d = [c(1).name, c(2).name, c(3).name] and
d=[] for a large struct to put the result together, but need to redivide
the result array. so maybe for loop is required.
and to create a struct matrix, maybe the for loop method or repmat way is needed.

Sign in to comment.

More Answers (1)

Alexandra Harkai
Alexandra Harkai on 4 Nov 2016
Edited: Alexandra Harkai on 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.


xiao on 5 Nov 2016
thx! so, for loop method is required to get the data of a struct array or matrix?
Alexandra Harkai
Alexandra Harkai on 7 Nov 2016
Please do not change your question once it has been answered, it is preferred to start new one.

Sign in to comment.

Sign in to answer this question.

Translated by