extract/access same fields from multiple structures (it is a nested structure)

2 views (last 30 days)
I have 9 structures (1*1 structures), each structure contains 45 fields. some of the field are single values such as : TKE 0.12 in the first structure, TKE 0.005 in the second structure and so on. I am trying to get the TKE fields form these structures and have them in one array (I wanna plot them later). I am not sure how to do it! could anyone please give me a quidance? (by the code: name_of_structure_one.TKE I can get the first TKE which is 0.12 but I want to have all the nine TKE in one array! (TKE is the name of one of the fields)
P.S I put all the 9 structure in one structure using load file_name.mat, thought I can work with it easier maybe! so now I have one structure which has 9 structures in it each structure has 45 fileds in it. how can I get one of the fileds from all 9 structures?
thanks a lot!

Accepted Answer

Walter Roberson
Walter Roberson on 8 Dec 2021
temp = [S1; S2; S3; S4; S5; S6; S7; S8; S9];
all_TKE = [temp.TKE];
The above would only work if the structures all have the same fields in the same order. Otherwise,
all_TKE = cellfun(@(S) S.TKE, {S1; S2; S3; S4; S5; S6; S7; S8; S9});
Narges Raee
Narges Raee on 13 Dec 2021
I have found a solution that does not nedd the names of the files! I put it here so people who search this question an answer can see it:
c = who ()
this will give you all the structures in a single structure and then you can get the field you are looking for easily!

Sign in to comment.

More Answers (0)




Community Treasure Hunt

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

Start Hunting!