How to accumulate different structs, with same fields beneath one another

I have a struct, which is similar format to the one below. I have 128 different people with the data in the same format, like the one below.
All I'm trying to do, is write a loop which will simply read in each participants struct (in the exact same format as below), and stack all of the results of the participants in one cotinuous vector for each specific field.
So eventually we would have one long field containing all of the field1 variables for all participants
One long field containing all of the field2 variables for all participants in the same struct...
All I can get at the moment, is the loop to replace the data existing in the struct, rather than add it underneath the previous participant's..
Please can someone help me?
Thanks
Cam
s = struct(field1,value1,field2,value2,field3,value3,field4,value4, field5,value5)
field1 = 'f1'; value1 = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'};
field2 = 'f2'; value2 = {1,2,3,4,5,6,7,8,9};
field3 = 'f3'; value3 = {13,32,3.2,42,5,26,7,38,9};
field4 = 'f4'; value4 = {1,2,3,34,35,6,7,83,9};
field5 = 'f5'; value5 = {1,4,11,45,5,6,17,28,79};

 Accepted Answer

i am not sure how exactly your multiple structs are saved, but if you saved them in a multidimensional struct, you can get all fields of a specific entries e.g. like following:
myStruct(1).name='Teo';
myStruct(1).age=24;
myStruct(2).name='Lea';
myStruct(2).age=35;
cellWithAllNames={myStruct.name};
if you did not save the structs in a m-dimensional struct you can just concatenate them like normal variables (e.g. [struct1; struct2]) and then call one field name for the overall struct like in the above code.

6 Comments

Hi Jonas,
Thanks for this.
My structs are saved in the same format as the example above, so they come into the loop in that format.
I can't work out your example I'm afriad (probably because I'm a matlab newbie)
Thansk
Cam
hi cam, if you saved it as m-d struct as above you can access the all f1 fields using {s.f1}. the resulting cell has as many entries as your dimensions of your struct. and each cell a 1x10 cell inside.
if this does not help you, you may append your data here, then you can get more specific help
Hi Jonas,
Thanks, so I've attached some example data here, which is in the same format as the actual data I have. I just want to read in each participants data, then store it in a struct, and read in the next participants and store their data in the same fields underneath
Thanks
Cam
so if i see that correctly: you have multiple files (one for each participant) and each file is a 1x9 struct. each of the 9 structs has 5 fields, where the first field f1 is a character array and the other 4 fields are single numbers.
what exactly is your wanted output? you can for example generate a Nx9 struct if you have N participants. to achieve this you could generate a struct using
myStruct=Test1.data;
and expand it with the next struct using
myStruct(2,:)=Test2.data;
and so on.
if you want to keep one 1x9 struct but each field is a Nx1 array or character array you can accumulate the values in the fields similarly like
myStruct=Test1.data;
fields=fieldsnames(myStruct);
for nrOfFile=1:N
for fieldNr=1:numel(fields)
load(['participant' num2str(nrOfFile)];
myStruct.(fields{fieldNr})=[myStruct.(fields{fieldNr}); ...
Test.data.(fields{fieldNr})];
end
end
if your m-d array corresponds to e.g. a trial number it may be nice to generate a table from your accumulated struct using struct2table()
Thanks mate, this is so helpful! I basically want to generate a single struct, which contains the data from all participants in each field. but this seems to have worked
Thanks again!
Cam
great if it worked! if you don't mind you can accept the answer to mark this question as answered

Sign in to comment.

More Answers (0)

Categories

Asked:

on 4 Jun 2021

Commented:

on 7 Jun 2021

Community Treasure Hunt

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

Start Hunting!