Matlab sorting numbers oddly
8 views (last 30 days)
Show older comments
Hello, I am working on a project with data taken at different points in time. I would like to import the data into a 3 dimensional array, with the 1st dimension being intensity, the 2nd wavelength, and the 3rd being time. When I import all the data however, MATLAB seems to be reading it oddly. In windows, all the data is neatly organized according to the timestamp (located in the last 3 digits of the file, corresponding to the seconds elapsed and starting at 0). 

Matlab reads it differently however, starting at 0 before jumping to 100-109, 10, 111-119, 11. . . 298, 299, 29, 2, 300-303, 30-39, 3. . . etc.


Is there any way to change the way it reads and sorts files, or maybe batch rename them in matlab? Thank you
0 Comments
Answers (2)
Walter Roberson
on 3 Jul 2025
Edited: Walter Roberson
on 3 Jul 2025
You probably want to use https://www.mathworks.com/matlabcentral/fileexchange/47434-natural-order-filename-sort
The filename order that MATLAB is using is whatever is returned by the operating system. In turn Windows relies on whatever is returned by the NTFS filesystem driver. The file sort order of NTFS is not documented, but experimentally it appears to sort based on byte order of filename representation. Byte order of filename representation apparently gets locked in according to the LANGUAGE environment setting of the person who created the NTFS filesystem -- so two different NTFS filesystem might have different sort orders.
The sorting appears to be by name moving from left to right -- and '_' sorts after the digits, so '1_' sorts after '19'
Paul
on 3 Jul 2025
Hi Elijah,
Would be helpful if you showed the code for reading the files.
If I'm understanding correctly, maybe you could use dir to get all of the file names, process the name field of the output of dir to get the names sorted by time stamp, and then read the files in sorted order?
0 Comments
See Also
Categories
Find more on Search Path 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!