Set multiple fields of a struct to a certain value?
이전 댓글 표시
Hi Guys,
I need to set multiple fields of a struct to a certain value.
For example:
pressdata(1).refF1 = 1000
pressdata(2).refF1 = 1000
pressdata(3).refF1 = 1000
pressdata(4).refF1 = 1000
Is there any way to make this more elegant within 1 line?
pressdata(1:4).refF1 = 1000
...does not work :(
A loop seems kind of overkill:
for x=1:4
pressdata(x).refF1 = 1000
end
Thanks,
Thomas
채택된 답변
추가 답변 (1개)
Daniel Catton
2022년 2월 15일
1 개 추천
You could do a loop but on just the one line?
for x=1:4; pressdata(x).refF1 = 1000; end
댓글 수: 1
Image Analyst
2022년 2월 15일
Perhaps this is more intuitive, and maintainable should you need to give the code to other people. Most people would have to look up what the deal() function does, while this is obvious.
카테고리
도움말 센터 및 File Exchange에서 Structures에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!