Sorting Nested Structures based on Name

Hi all,
I have a code that imports folders full of data files and organizes/processes them based on file type. As it currently works, the data gets organized into a struct that has multiple sub-structs containing measurement data. I need the rows of data sorted by name but have not found a way to get the sub-structs sorted as well.
Currently, the data looks like this when imported:
I'd like to reorder the data so that the rows are sorted by name, but I can only get the main struct sorted this way, not the sub-structs as well. I'm using R2018b if that helps.

 Accepted Answer

Stephen23
Stephen23 on 21 Oct 2021
Edited: Stephen23 on 21 Oct 2021
As far as I can tell, your actual goal is to sort the elements of all structures based on the content of their NAME field.
Assuming that all names have sufficient leading zeros, there are no negatives, decimal fractions, etc, and (within one structure) all values use the same units... then something like this should get you started:
[~,idx] = sort({rateshape.name});
rateshape = rateshape(idx);
for k = 1:numel(rateshape)
[~,idx] = sort({rateshape(k).data.name});
rateshape(k).data = rateshape(k).data(idx);
end
If there are different units then you could download this:
otherwise if there are insufficient leading zeros or negative/exponent/fractional values then you will need parse the text into numeric.

1 Comment

That worked, thank you! I have a piece of code already that adds the leading zeros so this works perfectly.

Sign in to comment.

More Answers (1)

You should be able to perform the same code you do on the top level to any beneath it using a for loop.
If you need a solution where you can't assume the search depth, it will be more complex and require recusion.
Example code:
mainStruct = orderfields(mainStruct);
fields = fieldnamse(mainStruct);
for i = 1:size(fields,1)
mainStruct.(fields{i}) = orderfields(mainStruct.(fields{i}));
end

5 Comments

Smith
Smith on 21 Oct 2021
Edited: Smith on 21 Oct 2021
Yeah this doesn't seem to work, I'll experiment with it though since this might be the way I need to go (just more complicated). Right now I get "Error using orderfields:Too many input arguments." The Main struct always has 10 rows, but the sub-structs vary in length from 1 to 20 rows. The data organization shouldn't change at all, so the number of fields is always constant.
EDIT: Actually this might not be the direction I was trying to go. I needed to re-order the rows to be in numerical order, not re-order the fields (since there's only two and I don't care about which order they are in)
So I'm not clear what you're looking for. My understanding is you have a top-level structure with N fields, then need to sort those struct fields based on the order of their names.
(I'll ignore the fact that the field names you listed aren't valid field names given the start with a number :) )
Instead, you want to reorder which rows?
Smith
Smith on 21 Oct 2021
Edited: Smith on 21 Oct 2021
Sorry about not being more clear. The original post now has pictures so it should be easier to see what I'm trying to do. I'm trying to reorganize the rows based on the field 'name'.
Stephen23
Stephen23 on 21 Oct 2021
Edited: Stephen23 on 21 Oct 2021
"I needed to re-order the rows to be in numerical order,"
What rows?
The only thing in your screenshots that have more than one row are the tables:
  • RATESHAPE structure has size 1x10 (i.e. only one row).
  • the field NAME is in all elements a 1xN character vector (i.e. only one row each).
  • the field DATA is in all elements a 1xN structure (i.e. only one row each).
  • the field DATA.NAME is in all elements a 1xN character vector (i.e. only one row each).
  • the field DATA.RATESHAPES is in elements a 256x3 table (i.e. 256 rows).
The tables are the only things with non-trivial numbers of rows. So you really want to sort the tables?
Maybe I'm using the wrong terminology, but I thought that when it shows 1x10 that it was 1 column and 10 rows. In the screenshot above the very first struct shows 1x13, and the second screenshot is of that exact sub-struct, which has 13 rows and one column. The question I had is answered though, thank you for the help.

Sign in to comment.

Categories

Products

Release

R2018b

Asked:

on 21 Oct 2021

Commented:

on 21 Oct 2021

Community Treasure Hunt

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

Start Hunting!