How to assign values to a struct array with fields?
99 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
采纳的回答
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
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Structures 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!