How can you move up data in a structure to a higher level?

20 views (last 30 days)
Hi all,
I was given a dataset that has 3 "levels" in the structure. One of the levels is redundant and I'm very sick of typing it. However, there's a lot of data in this structure and it would take me forever to rename individually.
Basically I have a structure like this:
Data (structure name)->Data 1, Data 2, Data 3, etc(1 level down, each Data here corresponds to a different research subject)-> Data (2 levels down)-> 50 variables (3rd level down).
Basically, I want to remove the 2nd 'Data' level because it adds no organization or information, and certain aspects of my code are ridiculously long because of it. I tried to just delete the 2nd data with a right click delete, of course that got rid of all the variables under it... which is all of them for Data 1, Data 2 etc. Even with a for loop for renaming all the data, I would have to have all 50 variables renamed just to run through each of the separated data.
Is there any easy way to just bump up the level that variables are on?
  2 Comments
Guillaume
Guillaume on 16 Jul 2018
Do the 50 fields all have the same names for each Dataxxx?
Are Data1, Data2, Data3, etc. actual field names of the upper scalar Data structure? or is there just a structure array Data(1), Data(2), etc.?
If it is not a structure array, then the whole thing needs changing to Data(i)->50 variables. Only 1 level of a structure array.
ADF
ADF on 16 Jul 2018
Yes, the 50 fields are the same between Data xxx. Each animal number (1 level down from strut name) is a separate 1x1 structure, under which there is the useless level which is another 1x1 structure. All variables of interest are under the useless 1x1 repetitive structure.

Sign in to comment.

Accepted Answer

Guillaume
Guillaume on 18 Jul 2018
First thing, let's convert all the dataxxx scalar structure into a structure array. The simplest way is to convert your upper level structure into a cell array (very fast) then concatenate all the cells which will form a structure array:
datacell = struct2cell(yourmainstructure);
dataarray = [data{:}];
So now, you should have a 1xNumber of animals structure with 1 useless Data field that is itself a structure that contain the useful data. To get rid of that extra level of indirection, again convert to a cell array (it should create a 1x1xNumer of animals cell array) and concatenate
datacell = struct2cell(dataarray);
dataarray = [datacell{:}];
This should result in a 1xNumber of animals structure with 50 fields. Easy to index.
And of course, you should fix whatever code generated that unnecessary complicated hierarchy in the firt place.
  1 Comment
ADF
ADF on 24 Jul 2018
Thanks! Yeah, I was just given a .mat file formatted like this, don't even have the source code. Appreciate it!

Sign in to comment.

More Answers (0)

Categories

Find more on View and Analyze Simulation Results 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!