Natural-Order Filename Sort

버전 3.4.5 (71.1 KB) 작성자: Stephen23
Alphanumeric sort of filenames or filepaths, with customizable number format.
다운로드 수: 29.5K
업데이트 날짜: 2023/7/13

라이선스 보기

편집자 메모: This file was selected as MATLAB Central Pick of the Week

To sort the elements of a string/cell array use NATSORT:
To sort the rows of a string/cell array use NATSORTROWS:
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)
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.
>> 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 =
>> natsortfiles(B) % Shorter names before longer:
ans =
%% Directory Names:
>> C = {'A2-old\test.m';'A10\test.m';'A2\test.m';'A1\test.m';''};
>> sort(C) % Wrong number order, and '-' sorts before '\':
ans =
>> natsortfiles(C) % Shorter names before longer:
ans =
>> 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 =
>> natsortfiles(F) % Shorter names before longer:
ans =

인용 양식

Stephen23 (2024). Natural-Order Filename Sort (, MATLAB Central File Exchange. 검색됨 .

MATLAB 릴리스 호환 정보
개발 환경: R2010b
R2009b 이상 릴리스와 호환
플랫폼 호환성
Windows macOS Linux
Help CenterMATLAB Answers에서 Text Files에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!
버전 게시됨 릴리스 정보

* Accept decimal comma as well as decimal point.
* HTML example use string arrays.


* Add testcases.


* Now R2009b compatible.


* Edit description & help.


* Edit description & help.


* Add plenty of testcases.
* Fix bug in descending sort with an empty input array.


* Improve test function, add test cases.
* Rename options nopath->xpath, nodot->rmdot


* Update TESTFUN.
* Fix bug in struct parsing.


* More robust TESTFUN pretty-print code.
* Add NODOT option.


* Improve examples.


* Correct summary.


* Improve string handling.


* Simplify numeric class handling.
* Add permutations test examples.


* handle single element with no number.


* 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.


* Consistent alignment tab/spaces.


* Add error IDs.
* Add "noext" option.


* Fix handling of char<num.


* NATSORT total rewrite: faster and less memory.
* Improve HTML documentation.
* Include testcases.

* Add (very useful) debugging output argument.

* Improve blurb and HTML.
* Improve input checking.
* Include NATSORT function.
* Add HTML documentation.
* Minor help edit.

* Clearer description of file dependency.
* Improve example of filepath sorting.

* Improve function description.
* Better examples.

- Update documentation only, improve examples.

- Complete acknowledgements.