图片缩略图

自然秩序文件名排序

版本3.3.0 (22.9 KB) 斯蒂芬
字母数字类型的文件名或文件路径,具有可定制的数字格式。

17.5 k下载

更新2021年7月14日

查看许可协议

编者按:此文件被选为Matlab Central本周精选

要对字符串/单元格数组进行排序,请使用natsort:
使用NATSORTROWS对字符串/单元格数组的行进行排序:
### 概括 ###
对字符串/单元格/结构数组中的文本进行字母数字排序。考虑到任意数字子字符串的值,对字符串进行排序。例如:比较
>> a = {'a2.txt'“a10.txt”“a1.txt”};
>>排序(a)
ans =“a1.txt”“a10.txt”'a2.txt'
> > natsortfiles (A)
ans =“a1.txt”'a2.txt'“a10.txt”
默认情况下,NATSORTFILES将所有连续的数字解释为整数,可以使用正则表达式指定数字子串识别:参见NATSORT了解详细信息。
natsortfiles不会执行天真的自然秩序,但单独排序文件名和文件扩展,以确保字典排序,其中较短的文件名始终在更长的内容中排序。同样在每个文件分隔字符下拆分文件路径,并且文件层次结构的每个级别都是单独排序的。
###示例使用dir ###
P =“C: \ SomeDir \子目录”
s = dir(fullfile(p,‘* . txt”));
S = natsortfiles(年代);
k = 1:元素个数(S)
F = fullfile (P, S (k) . name)
结束
###文件依赖###
自然顺序排序由函数NATSORT (File Exchange 34464)提供。NATSORT的所有可选输入都由NATSORTFILES支持。金宝app
### 例子 ###
>> B = {'test2.m'“test10-old.m”“test.m”'test10.m''test1.m'};
> >排序(B)错误的号码顺序:
ans =
“test.m”
'test1.m'
“test10-old.m”
'test10.m'
'test2.m'
> > natsortfiles (B)%较短的名称更长:
ans =
“test.m”
'test1.m'
'test2.m'
'test10.m'
“test10-old.m”
%%目录名称:
>> c = {“A2-old \ test.m”'a10 \ test.m'“A2 \ test.m”“A1 \ test.m”“A1-archive.zip”};
> >排序(C)%数字顺序错误,'-'排序在'\'之前:
ans =
“A1-archive.zip”
'a10 \ test.m'
“A1 \ test.m”
“A2-old \ test.m”
“A2 \ test.m”
> > natsortfiles (C)%较短的名称更长:
ans =
“A1 \ test.m”
“A1-archive.zip”
“A2 \ test.m”
“A2-old \ test.m”
'a10 \ test.m'
>> d = {“A1 \ B”'a + / b'“A / B1”“= / B '“\ B0”};
> >排序(D)
ans ='a + / b'“A / B1”“A1 \ B”“= / B '“\ B0”
> > natsortfiles (D)
ans =“\ B0”“A / B1”“A1 \ B”'a + / b'“= / B '
>> f = {'test_new.m'“test-old.m”“test.m”};
> >排序(F)%注意'-'排序在'.'之前:
ans =
“test-old.m”
“test.m”
'test_new.m'
> > natsortfiles (F)%较短的名称更长:
ans =
“test.m”
“test-old.m”
'test_new.m'

引用作为

斯蒂芬(2021)。自然秩序文件名排序(//www.tatmou.com/matlabcentral/fileexchange/47434-nattural-order-filename-sort),Matlab中央文件交换。检索到