MATLAB not sorting the text files in order.

9 views (last 30 days)
Hello,
I have a very simple question which I am trying to find the solution online, but it is really something not appearing anywhere. Probably its a silly one but I am kind of stuck.
  1. I am trying export a large number of text files and plot them. But the problem is that Matlab is not sorting them in order and because of that my calculations are getting a bit complicated. Can anyone suggest an easy way to fix this. Thanks.
  2 Comments
the cyclist
the cyclist on 27 Feb 2023
Edited: the cyclist on 27 Feb 2023
MATLAB is sorting those correctly -- in dictionary ordering. Note that "2.00" comes before "20.00", because the decimal point comes before the zero, but after "18.00" because 2 comes after 1.
It sounds like you may already have a "complicated" solution, but one possibility is to extract just the numerical part of the strings you show, perhaps using regular expressions (i.e. regexp), which you would be able to sort numerically.
If you upload those file names, it would be helpful in suggesting a specific solution.
Stephen23
Stephen23 on 27 Feb 2023
Edited: Stephen23 on 27 Feb 2023
"I have a very simple question which I am trying to find the solution online, but it is really something not appearing anywhere."
That order is known as "natural order sort":
and in MATLAB there are a number of third-party functions which perform this sorting:

Sign in to comment.

Accepted Answer

Stephen23
Stephen23 on 27 Feb 2023
Edited: Stephen23 on 27 Feb 2023
You can DOWNLOAD and use my code NATSORTFILES:
Because you have decimal fractions you will need to specify the optional 2nd input argument as well, e.g.:
S = dir(..);
S = natsortfiles(S,'[-+]?\d+\.?\d*')
The NATSORTFILES documentation has a detailed explanation of how to use NATSORTFILES and its options.
  3 Comments
Steven Lord
Steven Lord on 15 Mar 2023
One easy way to get this add-on is to click on the Get Add-Ons button on the Home tab of the toolstrip (the icon looks like three cubes.) In the Add-Ons Explorer that comes up search for "natural sort". Select the first hit in the search then press the Add button on that add-on's page.
Stephen23
Stephen23 on 15 Mar 2023
Edited: Stephen23 on 15 Mar 2023
"However, the natsortfiles seems like wont work without its .m file. Is it okay to ask you where can I get that ?"
I already gave you the link in my answer, and wrote that you need to DOWNLOAD it.
When you open that page, there is a big blue DOWNLOAD button in the top right corner.
Click on the big blue DOWNLOAD button.
Save the zip file on your computer (e.g. in your DOWNLOADS directory).
Unzip the zip file onto your MATLAB search path, e.g. into the current directory.
Call the function as I showed in my answer.

Sign in to comment.

More Answers (1)

Walter Roberson
Walter Roberson on 27 Feb 2023
MATLAB is sorting correctly.
The first character is compared before any other characters are compared, so all entries starting with '5' come before all entries starting with '6' -- so '58' comes before '6.0' .
The question after that is whether '6.' comes before '60' or after '69' and the answer is that '6.' comes before '60' . So you get exactly the order you noted, where '58' comes before '6.' and that comes before '60' .
You are expecting MATLAB to be able to automatically figure out that you do not want it to sort by text order and instead expect it to sort by text order until the point where it should "just know" to switch to sorting by the order of the number represented by the text. That is not going to happen.

Categories

Find more on Downloads 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!