How to nat sort rows of a struct

Hi,
I have a matlab script that I am developing for analysing data. After completing some testing last week I have come into the same issue as many others that when I have listed my files that have not been in numerical order. Currently, my files are in this order:
However I need to reorder the rows to have them listed in numerical order with the contents of the rows staying together:
'Emission_1.txt'
'Emission_2.txt'
'Emission_3.txt'
'Emission_4.txt'
'Emission_5.txt'
'Emission_6.txt'
'Emission_7.txt'
'Emission_8.txt'
'Emission_9.txt'
'Emission_10.txt'
'Emission_11.txt'
'Emission_12.txt'
The output struct that I get at the moment looks like
I am really struggling to get them into the correct order while keeping the struct in the same layout. I would really prefer to does as the rest of my script is now built up and I would prefer not to have to change it.
I have looked into using things like natsort, sort_nat and nestedSortStruct but I can't seem to get the output that I need.
Is there a way that this can be done?
I will put my code with attachments too to try and help

1 Comment

"I am really struggling to get them into the correct order..."
Download my FEX submission natsortfiles, unzip it onto the search path, and then simply use it like this:
S = dir(..);
S = natsortfiles(S);
... your code

Sign in to comment.

 Accepted Answer

FileList = dir(fullfile(Folder, '*.*'));
[~, Index] = natsort({FileList.name});
FileList = FileList(Index);
If the file extensions should not influence the sorting order, use natsortfiles from the same link.

5 Comments

"If the file extensions should not influence the sorting order..."
Well actually they still do, in the sense that .mp4 will sort after .mp3... the difference is that natsortfiles sorts the names and extensions separately and thus provides a so-called dictionary sort, so that shorter filenames sort before longer ones, e.g. name1.m before name1-new.m
I have misunderstood the part "so that the file extension character does not influence the sort output" of the actually clear explanation:
NATSORTFILES does not perform a naive natural-order sort, but sorts the filenames and file extensions separately so that the file extension character does not influence the sort output. This ensures a dictionary sort, where shorter filenames always sort before longer ones.
Stephen23
Stephen23 on 19 Mar 2018
Edited: Stephen23 on 19 Mar 2018
@Jan: by the "file extension character" I just mean the period character . which is implicitly ignored when the file names and extensions are split. Maybe I should make that more explicit: " file extension separator character" perhaps?
That is great. Thank you both so much for your help, that has done exactly what I wanted!
@Stephen: Or "so that the file extension character (the dot) does not". I read "the file extensions character*s*", but this was obviously my mistake.

Sign in to comment.

More Answers (0)

Categories

Asked:

on 19 Mar 2018

Edited:

on 4 May 2021

Community Treasure Hunt

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

Start Hunting!