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

55 views (last 30 days)
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

Jonas
Jonas on 4 Jun 2021
Edited: Jonas on 4 Jun 2021
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
Cameron Kirk
Cameron Kirk on 7 Jun 2021
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
Jonas
Jonas on 7 Jun 2021
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

Find more on Structures 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!