Why do the DIR and LS functions in MATLAB not sort numerical filenames in numerical order?

조회 수: 33 (최근 30일)
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.

채택된 답변

MathWorks Support Team
MathWorks Support Team 2009년 6월 27일
The DIR or LS functions in MATLAB sort the strings in ASCII dictionary order. This can cause a problem if you want to sort numbered files which do not have leading zeros.
Currently, to work around this problem, you can use the text manipulation and sorting routines in MATLAB.
Here is an example:
files=dir('*.tif');
numfiles = size(files,1); % Find number of files
numdelim = 3; % Number of delimiters
delims = ['-' '.']; % Delimiters used
% Create a matrix of the file list index and the delimiters
filenums=[ [1:numfiles]' zeros(numfiles,3) ];
for i=1:numfiles % Cycles through list of files
rem=files(i).name;
for j=1:numdelim % Cycles through the filename delimiters
[token,rem] = strtok(rem,delims);
filenums(i,j+1) = str2num(token);
end
end
% Sort the matrix by rows
filenums = sortrows(filenums,[2:numdelim+1]);
% Show the filenames in the new order
files(filenums(:,1)).name
% Create a cell array of the filenames in new order
for i=1:numfiles
sortedfiles{i,1} = files(filenums(i,1)).name;
end
  댓글 수: 4
Stephen23
Stephen23 2021년 5월 17일
편집: Stephen23 2021년 8월 16일
"The DIR or LS functions in MATLAB sort the strings in ASCII dictionary order."
Easily disproved by actually running LS and DIR on a few very basic filenames:
ls *.txt
10.txt 1.txt 2.txt
dir *.txt
1.txt 10.txt 2.txt
Note that those are different orders.
Different orders means it is not possible for both of them to be "in ASCII dictionary order" as this answer (most likely incorrectly) states that MATLAB sorts them into. One of them does happen to be in ASCII order, but certainly not both of them (unless MST has invented a completely new, undocumented, variable ASCII order).
Walter Roberson
Walter Roberson 2021년 8월 13일
The R2021a documentation for dir() and ls() do not document any ordering.
ASCII dictionary order is not likely, as the supported operating systems use various forms of unicode.
I just did a test on my Mac;
!touch q R
ls
q R
On my Mac, R was before q -- which is consistent with sorting by codepoint, but is not consistent with ascii dictionary order.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Stephen23
Stephen23 2017년 1월 29일
편집: Stephen23 2021년 4월 18일
Simple solution: download my FEX submission natsortfiles, which sorts filenames into alphanumeric order:
S = dir('*.txt');
S.name
ans = '1.txt'
ans = '10.txt'
ans = '2.txt'
S = natsortfiles(S); % alphanumeric sort by filename
S.name
ans = '1.txt'
ans = '2.txt'
ans = '10.txt'

카테고리

Help CenterFile Exchange에서 File Operations에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by