【原始碼】檔名/檔案路徑的自然排序函式NATSORTFILES version 1.6.0.0
函式集NATSORTFILES對檔名或檔案路徑(1xN char)的單元陣列進行排序,同時考慮字串中的所有數字。
The function NATSORTFILES sorts a cellarray of filenames or filepaths (1xN char), taking into account any numbervalues within the strings.
這被稱為自然順序排序或字母數字排序。
This is known as a natural order sort or analphanumeric sort.
請注意,MATLAB的內建排序函式SORT只對字元程式碼進行排序(與大多數程式語言中的排序一樣)。
Note that MATLAB’s inbuilt SORT functionsorts the character codes only (as does SORT in most programming languages).
NATSORTFILES不是一種簡單的自然順序排序,而是將檔名和副檔名分開後進行分類,這意味著NATSORTFILES將短檔名排列在長檔名之前,即字典排序。
NATSORTFILES is not a naive natural-ordersort, but splits and sorts filenames and file extensions separately, whichmeans that NATSORTFILES sorts shorter filenames before longer ones: this isknown as a dictionary sort.
出於同樣的原因,在每個路徑分隔字元(’’ 或 ‘/’)處分割檔案路徑,每個目錄級別被單獨排序。
For the same reason filepaths are split atevery path-separator character (either ‘’ or ‘/’), and each directory level issorted separately.
使用NATSORTROWS函式排序字串單元陣列的行。
For sorting the rows of a cell array ofstrings use NATSORTROWS.
使用NATSORT排序字串單元陣列。
For sorting a cell array of strings useNATSORT.
基本用法
預設情況下,NATSORTFILES將連續數字認為是單個整數的一部分,每個數字被認為與一個字母的寬度相同:
By default NATSORTFILES interpretsconsecutive digits as being part of a single integer, each number is consideredto be as wide as one letter:
A = {‘a2.txt’, ‘a10.txt’, ‘a1.txt’};
sort(A)
natsortfiles(A)
ans =
‘a1.txt’ ‘a10.txt’ ‘a2.txt’
ans =
‘a1.txt’ ‘a2.txt’ ‘a10.txt’
排序索引
第二個輸出引數是排序索引ndx的數字陣列:
The second output argument is a numericarray of the sort indices ndx, such that Y = X(ndx) where Y = natsortfiles(X):
[~,ndx] = natsortfiles(A)
ndx =
3 1 2
除錯陣列
第三個輸出引數是單元陣列的單元格向量,其中每個單元陣列包含單個字元和數字(在轉換為數字之後)。
The third output is a cell vector of cellarrays, where each cell array contains individual characters and numbers (afterconverting to numeric).
這對於確認數字已經被正常表示式正確識別是有用的。
This is useful for confirming that thenumbers are being correctly identified by the regular expression.
單元向量的單元格對應於分割後的目錄、檔名和副檔名。
The cells of the cell vector correspond tothe split directories, filenames, and file extensions.
[,,dbg] = natsortfiles(A);
dbg{:}
ans =
‘a’ [ 2]
‘a’ [10]
‘a’ [ 1]
ans =
‘.’ ‘t’ ‘x’ ‘t’
‘.’ ‘t’ ‘x’ ‘t’
‘.’ ‘t’ ‘x’ ‘t’
DIR與單元陣列舉例
使用DIR找出資料夾中的所有檔案,並對它們進行正確排序,然後依次迴圈遍歷,舉例如下。
One common situation is to use DIR toidentify files in a folder, sort them into the correct order, and then loopover them: below is an example of how to do this.
D = ‘natsortfiles_test’; % directory path
S = dir(fullfile(D,’*.txt’)); % get list of files in directory
N = natsortfiles({S.name}); % sort file names into order
for k = 1:numel(N)
disp(fullfile(D,N{k}))
end
natsortfiles_test\A_1.txt
natsortfiles_test\A_1-new.txt
natsortfiles_test\A_1_new.txt
natsortfiles_test\A_2.txt
natsortfiles_test\A_3.txt
natsortfiles_test\A_10.txt
natsortfiles_test\A_100.txt
natsortfiles_test\A_200.txt
DIR與結構體
需要訪問DIR結構體的使用者可以使用NATSORTFILE的第二個輸出引數,並對該輸出結構體進行正確排序:
Users who need to accessthe DIR structure fields can use NATSORTFILE’s second output tosort DIR’s output structure into the correct order:
D = ‘natsortfiles_test’; % directory path
S = dir(fullfile(D,’*.txt’)); % get list of files in directory
[~,ndx] = natsortfiles({S.name}); % indices of correct order
S = S(ndx); % sort structure using indices
for k = 1:numel(S)
fprintf(’%-13s%s\n’,S(k).name,S(k).date)
end
A_1.txt 22-Jul-2017 09:13:23
A_1-new.txt 22-Jul-2017 09:13:23
A_1_new.txt 22-Jul-2017 09:13:23
A_2.txt 22-Jul-2017 09:13:23
A_3.txt 22-Jul-2017 09:13:23
A_10.txt 22-Jul-2017 09:13:23
A_100.txt 22-Jul-2017 09:13:23
A_200.txt 22-Jul-2017 09:13:23
字典排序
B = {‘test_ccc.m’; ‘test-aaa.m’; ‘test.m’; ‘test.bbb.m’};
sort(B) % ‘-’ sorts before ‘.’
natsort(B) % ‘-’ sorts before ‘.’
natsortfiles(B) % correct dictionary sort
ans =
‘test-aaa.m’
‘test.bbb.m’
‘test.m’
‘test_ccc.m’
ans =
‘test-aaa.m’
‘test.bbb.m’
‘test.m’
‘test_ccc.m’
ans =
‘test.m’
‘test-aaa.m’
‘test.bbb.m’
‘test_ccc.m’
關於“檔名”的解釋
C = {‘test2.m’; ‘test10-old.m’; ‘test.m’; ‘test10.m’; ‘test1.m’};
sort© % Wrong numeric order.
natsort© % Correct numeric order, but longer before shorter.
natsortfiles© % Correct numeric order and dictionary sort.
ans =
‘test.m’
‘test1.m’
‘test10-old.m’
‘test10.m’
‘test2.m’
ans =
‘test.m’
‘test1.m’
‘test2.m’
‘test10-old.m’
‘test10.m’
ans =
‘test.m’
‘test1.m’
‘test2.m’
‘test10.m’
‘test10-old.m’
關於“檔案路徑”的解釋
D = {‘A2-old\test.m’;‘A10\test.m’;‘A2\test.m’;‘AXarchive.zip’;‘A1\test.m’};
sort(D) % Wrong numeric order, and ‘-’ sorts before ‘’:
natsort(D) % correct numeric order, but longer before shorter.
natsortfiles(D) % correct numeric order and dictionary sort.
ans =
‘A10\test.m’
‘A1\test.m’
‘A2-old\test.m’
‘A2\test.m’
‘AXarchive.zip’
ans =
‘A1\test.m’
‘A2-old\test.m’
‘A2\test.m’
‘A10\test.m’
‘AXarchive.zip’
ans =
‘AXarchive.zip’
‘A1\test.m’
‘A2\test.m’
‘A2-old\test.m’
‘A10\test.m’
MATLAB原始碼下載地址:
http://page5.dfpan.com/fs/4lcdj2221329b160da1/
更多精彩文章請關注微訊號: