natural-凯发k8网页登录

natural-order filename sort

版本 3.4.4 (24.4 kb) 作者: stephen23
alphanumeric sort of filenames or filepaths, with customizable number format.

25.4k 次下载

更新时间 2022/1/31

查看许可证

编者注: this file was selected as matlab central

to sort the elements of a string/cell array use natsort:
to sort the rows of a string/cell array use natsortrows:
summary
alphanumeric sort the text in a string/cell/structure array. sorts the text by character code taking into account the values of any number substrings. compare for example:
>> a = {'a2.txt', 'a10.txt', 'a1.txt'};
>> sort(a)
ans = 'a1.txt''a10.txt''a2.txt'
>> natsortfiles(a)
ans = 'a1.txt''a2.txt''a10.txt'
by default natsortfiles interprets all consecutive digits as integer numbers, the number substring recognition can be specified using a regular expression: see natsort for details.
natsortfiles does not perform a naive natural-order sort, but sorts the filenames and file extensions separately to ensure a dictionary sort, where shorter filenames always sort before longer ones. likewise filepaths are split at each file-separator character, and each level of the file hierarchy is sorted separately.
example with dir()
p = 'c:\somedir\subdir';
s = dir(fullfile(p,'*.txt'));
s = natsortfiles(s);
for k = 1:numel(s)
f = fullfile(p,s(k).name)
end
file dependency
the natural-order sort is provided by the function natsort (file exchange 34464). all of natsort's optional inputs are supported by natsortfiles.
examples
>> a = {'a2.txt', 'a10.txt', 'a1.txt'}
>> sort(a)
ans = 'a1.txt''a10.txt''a2.txt'
>> natsortfiles(a)
ans = 'a1.txt''a2.txt''a10.txt'
>> b = {'test2.m'; 'test10-old.m'; 'test.m'; 'test10.m'; 'test1.m'};
>> sort(b) % wrong number order:
ans =
'test.m'
'test1.m'
'test10-old.m'
'test10.m'
'test2.m'
>> natsortfiles(b) % shorter names before longer:
ans =
'test.m'
'test1.m'
'test2.m'
'test10.m'
'test10-old.m'
%% directory names:
>> c = {'a2-old\test.m';'a10\test.m';'a2\test.m';'a1\test.m';'a1-archive.zip'};
>> sort(c) % wrong number order, and '-' sorts before '\':
ans =
'a1-archive.zip'
'a10\test.m'
'a1\test.m'
'a2-old\test.m'
'a2\test.m'
>> natsortfiles(c) % shorter names before longer:
ans =
'a1\test.m'
'a1-archive.zip'
'a2\test.m'
'a2-old\test.m'
'a10\test.m'
>> d = {'a1\b', 'a /b', 'a/b1', 'a=/b', 'a\b0'};
>> sort(d)
ans = 'a /b''a/b1''a1\b''a=/b''a\b0'
>> natsortfiles(d)
ans = 'a\b0''a/b1''a1\b''a /b''a=/b'
>> f = {'test_new.m'; 'test-old.m'; 'test.m'};
>> sort(f) % note '-' sorts before '.':
ans =
'test-old.m'
'test.m'
'test_new.m'
>> natsortfiles(f) % shorter names before longer:
ans =
'test.m'
'test-old.m'
'test_new.m'

引用格式

stephen23 (2023). natural-order filename sort (https://www.mathworks.com/matlabcentral/fileexchange/47434-natural-order-filename-sort), matlab central file exchange. 检索来源 .

matlab 版本兼容性
创建方式 r2010b
与 r2009b 及更高版本兼容
平台兼容性
windows macos linux
类别
  • > > > > >
在 和 中查找有关 text files 的更多信息
标签
致谢

参考作品: , , , , , , , , ,

启发作品: , , , , , ,

community treasure hunt

find the treasures in matlab central and discover how the community can help you!

start hunting!
版本 已发布 发行说明
3.4.4

* add testcases.

3.4.3

* now r2009b compatible.

3.4.2

* edit description & help.

3.4.1

* edit description & help.

3.4.0

* add plenty of testcases.
* fix bug in descending sort with an empty input array.

3.3.0

* improve test function, add test cases.
* rename options nopath->xpath, nodot->rmdot

3.2.0

* update testfun.
* fix bug in struct parsing.

3.1.0

* more robust testfun pretty-print code.
* add nodot option.

3.0.5

* improve examples.

3.0.4

* correct summary.

3.0.3

* improve string handling.

3.0.2

* simplify numeric class handling.
* add permutations test examples.

3.0.1

* handle single element with no number.

3.0.0

* accepts and sorts the structure returned by dir.
* accepts and sorts a string array, categorical array, cell array of char, etc.
* regular expression and optional arguments may be string or char.

2.1.2

* consistent alignment tab/spaces.

2.1.1

* add error ids.
* add "noext" option.

2.1.0

* fix handling of char

2.0.0

* natsort total rewrite: faster and less memory.
* improve html documentation.
* include testcases.

1.6.0.0

* add (very useful) debugging output argument.

1.5.0.0

* improve blurb and html.
* improve input checking.
* include natsort function.
* add html documentation.
* minor help edit.

1.4.0.0

* clearer description of file dependency.
* improve example of filepath sorting.

1.3.0.0

* improve function description.
* better examples.

1.2.0.0

- update documentation only, improve examples.

1.1.0.0

- complete acknowledgements.

1.0.0.0
网站地图