Creating a variable with the values in the same fields of different structures

Hi,
I am writing an app where I want to compare the results stored in different mat sessions, say S1.mat, S2.mat,....... (the names are in reality more complex but this is not the point).
The number of sessions is not fixed, it may vary. Each session contains a structure, say I1, I2,....
These structures have all the same fields, I copy below only same of that:
Q: 9
Ifx: [100×1 double]
T: 180
P: [12×1 struct]
Pars: [12×1 double]
After loading the (not fixed) number of the structures, I want to create new varibales by combining the values having the same fields, for instance:
1) Qnew=[I1.Q, I2.Q,...In.Q]; % combining values in field Q
Is there a command for that?
2) Is possible to extend this to the structures too?
In this case I need to recover the names of the fields and to create a variable wiht the same name:
A1=[I1.P.A1, I2.P.A2,...In.P.An];
Thank you in advance!

 Réponse acceptée

Well, I won't give you a solution that creates separate variables, since there are standard hazards associated with that. However, the attached file will probably be of use to you. Example,
I(1).a=1; I(1).b=10; I(1).c=100;
I(2).a=2; I(2).b=20; I(2).c=200;
I(3).a=3; I(3).b=30; I(3).c=300;
out=scalarize_struct(I)
out = struct with fields:
a: [1 2 3] b: [10 20 30] c: [100 200 300]

6 commentaires

Well, I won't give you a solution that creates separate variables, since there are standard hazards associated with that.
Although, see this FEX contribution,
Thank you first of all!
I think that your suggestion can be useful, but I am not sure that I can use in this straightforward way as you do.
I have in fact different and distinct structures (I1, I2, ...In) with the same fileds. Your routine takes only one structure.
I could peraphs bypass the problem by creating a new structure containing all my distinct structures and then applying your code?
I am not very expert, sorry.
If the I1,I2,..In are already in your workspace, you should just concatenate them
out=scalarize_struct([I1,I2,...,In])
You mentioned they were stored in .mat files, S1.mat,..,Sn.mat. If n=10, you could compose them into a struct array as follows
n=10;
Slist="S"+(1:n);
Ilist="I"+(1:n);
I=arrayfun(@(s,i)load(s).(i), Slist, Ilist);
out=scalarize_struct(I)
In future, I would recommend saving them to the .mat files with the same variable name.
Ferdi's comment moved here,
Of course you are right: If I concatenate it works.
I use this exchange to learn something.
The command "S"+1:n does not work for me because of the symbol :.
It works with a for loop.
Which is the advantage to use the same variable name for I1, I2, ...In ?
To vaoid the command Ilist="I"+1:n; ?
Thanks!
The command "S"+1:n does not work for me because of the symbol :.
Shouldn't be a problem with my latest edit.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

Produits

Version

R2021b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by