I am trying to update a field value in a struct array. For example if I have 1*10 struct of A with a field in it called B, I want to replace the following loop with another method:
for iLoop=1:10
A(iLoop).B = 1;
end
The best thing I found is to use the setfield() function as:
A = setfield(A,num2cell(1:10),'B',num2cell(1),1);
This results in a 10-D struct which I cannot understand why. Does anyone know a way of doing this?

 채택된 답변

Walter Roberson
Walter Roberson 2011년 4월 8일

0 개 추천

[A(1:10).B] = 1;

댓글 수: 4

Jiro Doke
Jiro Doke 2011년 4월 8일
I think it should be
[A(1:10).B] = deal(1);
Hmmm, yes, you are right.
What if
A(1).B=[ 0 0 0 0 0], A(2).B=[0 0 0]...A(10).B=[0 0 0 0]
and
list=[ 4, 2,...,3 ]
The how would you change the indices specified in list such that
A(1).B=[0 0 0 1 0], A(2).B=[0 1 0]...A(10).B=[0 0 1 0]
?
Guillaume
Guillaume 2015년 3월 16일
Rather than hijacking a very old question, start your own question. Gives somebody a chance to get the credits for the answer.

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

추가 답변 (0개)

카테고리

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

질문:

2011년 4월 8일

댓글:

2015년 3월 16일

Community Treasure Hunt

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

Start Hunting!

Translated by