creating new fields in multiple structures with loops
Afficher commentaires plus anciens
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?
Réponse acceptée
Plus de réponses (1)
Fangjun Jiang
le 16 Mar 2020
Modifié(e) : Fangjun Jiang
le 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
Catégories
En savoir plus sur Variables dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!