creating new fields in multiple structures with loops
显示 更早的评论
Hi all
I have some data in the form of structures, eg strA, strB e.t.c. All structures have the same fields, let's say f1 and f2. I want to create the same new fields in every structure, for example:
strA.mean = mean(strA.f1);
strA.std = std(strA.f1);
Is there a way to do this with a loop so I don't have to write the above two lines of code for each structure? I tried the following but it doesn't work:
structnames = {'strA','strB','strC','strD','strF','strG','strK'}
n = length(structnames)
for i = 1:n
structnames{i}.mean = mean(structnames{i}.f1);
structnames{i}.std = std(structnames{i}.f1);
end
Any ideas?
采纳的回答
更多回答(1 个)
Fangjun Jiang
2020-3-16
编辑:Fangjun Jiang
2020-3-16
0 个投票
This is the time to use structure array, not to use strA, strB, strC, ...
Just like use array A=1:3, not to use A1=1, A2=2, A3=3.
If you had strA, strB, strC, etc. from somewhere else, then you could do
str=[strA;strB;strC] and follow the code by @Ameer Hamza
类别
在 帮助中心 和 File Exchange 中查找有关 Variables 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!