creating new fields in multiple structures with loops
Show older comments
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?
Accepted Answer
More Answers (1)
Fangjun Jiang
on 16 Mar 2020
Edited: Fangjun Jiang
on 16 Mar 2020
0 votes
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
Categories
Find more on Variables in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!