Editor's Note:This file was selected as MATLAB CentralPick of the Week
文件名或filepaths使用NATSORTFIL进行排序ES:
//www.tatmou.com/matlabcentral/fileexchange/47434-natural-order-filename-sort
To sort all of the strings in a cell array use NATSORT:
//www.tatmou.com/matlabcentral/fileexchange/34464-customizable-natural-order-sort
### Summary ###
Alphanumeric sort of the rows of a cell array of strings (1xN char). Similar to MATLAB's SORTROWS function, but takes into account the values of any numeric substrings occurring within the strings. Compare for example:
>> X = {'x2','10';'x10','0';'x1','0';'x2','2'};
>> sortrows(X) % Wrong numeric order
ans =
'x1' '0'
'x10' '0'
'x2' '10'
'x2' '2'
>> natsortrows(X) % Correct numeric order
ans =
'x1' '0'
'x2' '2'
'x2' '10'
'x10' '0'
By default NATSORTROWS interprets all consecutive digits as integer numbers, the number substring recognition can be specified using a regular expression: see NATSORT for details.
### File Dependency ###
The natural-order sort is provided by the function NATSORT (File Exchange 34464). All of NATSORT's optional inputs are supported by NATSORTROWS.
### Examples ###
>> A = {'B','2','X';'A','100','X';'B','10','X';'A','2','Y';'A','20','X'};
>> sortrows(A) % wrong numeric order:
ans =
'A' '100' 'X'
'A' '2' 'Y'
'A' '20' 'X'
'B' '10' 'X'
'B' '2' 'X'
>> natsortrows(A)
ans =
'A' '2' 'Y'
'A' '20' 'X'
'A' '100' 'X'
'B' '2' 'X'
'B' '10' 'X'
>> natsortrows(A,[],'descend')
ans =
'B' '10' 'X'
'B' '2' 'X'
'A' '100' 'X'
'A' '20' 'X'
'A' '2' 'Y'
>> sortrows(A,[2,-3]) % Wrong numeric order:
ans =
'B' '10' 'X'
'A' '100' 'X'
'A' '2' 'Y'
'B' '2' 'X'
'A' '20' 'X'
>> natsortrows(A,[],[2,-3])
ans =
'A' '2' 'Y'
'B' '2' 'X'
'B' '10' 'X'
'A' '20' 'X'
'A' '100' 'X'
>> B = {'ABCD';'3e45';'67.8';'+Inf';'-12';'+9';'NaN'};
>> sortrows(B) % wrong numeric order:
ans =
'+9'
'+Inf'
'-12'
'3e45'
'67.8'
'ABCD'
'NaN'
>> natsortrows(B,'[-+]?(NaN|Inf|\d+\.?\d*(E[-+]?\d+)?)')
ans =
'-12'
'+9'
'67.8'
'3e45'
'+Inf'
'NaN'
'ABCD'
Stephen Cobeldick (2020).Natural-Order Row Sort(//www.tatmou.com/matlabcentral/fileexchange/47433-natural-order-row-sort), MATLAB Central File Exchange. Retrieved.
Inspired by:sort_nat: Natural Order Sort,Number to Scientific Prefix,Customizable Natural-Order Sort,Number to Words,Natural-Order Filename Sort,Words to Number,Scientific Prefix to Number
Inspired:Words to Number,Customizable Natural-Order Sort,Number to Words,Natural-Order Filename Sort,Interactive Regular Expression Tool
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Create scripts with code, output, and formatted text in a single executable document.
@Brian Oisted: converting perfectly good numeric data to CHAR is unlikely to be a good approach, and as such NATSORTROWS is not the right tool for that task. Most likely you should use NATSORTFILES just on the filenames (not on the entire cell array).
For example, where C is your cell array (the first column contains the filenames):
[~,idx] = natsortfiles(C(:,1));
C = C(idx,:);
Windows does not document the order that it displays filenames in, so reproducing the Windows order is not a trivial task.
Hi Stephen,
谢谢你写这个函数,它真的帮助ps!
I am attempting to use the natsortrows function to sort a cell array where the first column is a series of file names and the next is the associated imported data. The data is in a 17x1 cell array, each cell containing a 1024x2 double of numbers. It would be great to sort these files 'naturally', as they show up in the Windows directory, but I'm unsure if the 17 1024x2 doubles need to be converted to character arrays, or if there is another workaround. Are you aware of any such workarounds, or methods of cell array (containing doubles) to character array conversions?
thank you very much, It's helpful
This is brilliant!
Thank you!
Thank you very much for this, it saved me a lot of time.
FYI at first I ran into some trouble with natsortrows saying my array must be a cell array of strings, then I tried sortrows which cleared things up by saying some cells in X contain non-scalar values. I had some empty cells left over in my array after running textscan. I was able to get it working using the following if it helps anyone else:
isArrayEmpty = cellfun('isempty',cellArray);
cellArray(isArrayEmpty) = {''};
cellArray = natsortrows(cellArray);
Thanks again