How to assign values to a struct array with fields?

Is there any way to write down a one-line script for assigning values to a struct array with fields?
x(1).a=1; x(2).a=2;
I'd like to change each value to 10 and 20 respectively. (I mean x(1).a=10, x(2).a=20) [x.a]=[10 20]; <- This causes an error.

 채택된 답변

Vandana Rajan
Vandana Rajan 2017년 1월 16일
편집: madhan ravi 2019년 6월 4일
Hi,
>> x = struct('a',{10,20});
This will create x(1).a = 10 and x(2).a = 20
To modify an already existing structure array,
>> new_val = num2cell([10,20]);
>> [x.a] = new_val{:};
Please see the following link

댓글 수: 2

Thanks for your comment, Vandana. It works perfectrly! But I found that there is no way to assign struct array in one line. :-(

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

추가 답변 (0개)

카테고리

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

태그

질문:

2017년 1월 13일

댓글:

2019년 6월 4일

Community Treasure Hunt

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

Start Hunting!

Translated by