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

조회 수: 47 (최근 30일)
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};

채택된 답변

Jonas
Jonas 2021년 6월 4일
편집: Jonas 2021년 6월 4일
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
Cameron Kirk
Cameron Kirk 2021년 6월 7일
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 2021년 6월 7일
great if it worked! if you don't mind you can accept the answer to mark this question as answered

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Structures에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by