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

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?

 채택된 답변

George
George 2016년 11월 4일
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 = c.name is equivalent to
d = c(1).name, c(2).name, c(3).name.
Try
[d,e,f] = c.name
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

thank you! inspired by you, i try d = [c(1).name, c(2).name, c(3).name] and
d=[c.name] 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.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Alexandra Harkai
Alexandra Harkai 2016년 11월 4일
편집: Alexandra Harkai 2016년 11월 4일

0 개 추천

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

xiao
xiao 2016년 11월 5일
편집: xiao 2016년 11월 5일
thx! so, for loop method is required to get the data of a struct array or matrix?
Please do not change your question once it has been answered, it is preferred to start new one.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Cell Arrays에 대해 자세히 알아보기

질문:

2016년 11월 4일

댓글:

2016년 11월 7일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by