Why do the DIR and LS functions in MATLAB not sort numerical filenames in numerical order?
Show older comments
The DIR and LS functions in MATLAB do not sort numerical filenames in numerical order.
For example, I have filenames of the form:
1-1-1.tif
1-1-2.tif
1-1-3.tif
. . .
1-1-9.tif
1-1-10.tif
1-1-11.tif
However, when I use DIR or LS to list the files using:
files=dir('*.tif')
The results stored in "dir.name" are:
1-1-1.tif
1-1-10.tif
1-1-11.tif
1-1-2.tif
1-1-3.tif
1-1-9.tif
I would like to order these numerically.
Accepted Answer
More Answers (1)
Simple solution: download my FEX submission natsortfiles, which sorts filenames into alphanumeric order:
S = dir('*.txt');
S.name
S = natsortfiles(S); % alphanumeric sort by filename
S.name
Categories
Find more on Language Support in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!