【源码】文件名/文件路径的自然排序函数NATSORTFILES version 1.6.0.0

【源码】文件名/文件路径的自然排序函数NATSORTFILES version 1.6.0.0_第1张图片

函数集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/

更多精彩文章请关注微信号:【源码】文件名/文件路径的自然排序函数NATSORTFILES version 1.6.0.0_第2张图片

你可能感兴趣的:(【源码】文件名/文件路径的自然排序函数NATSORTFILES version 1.6.0.0)