Rearranging filenames with extension .number (Ordering)

3 ビュー (過去 30 日間)
William
William 2015 年 4 月 24 日
編集済み: Stephen23 2015 年 4 月 24 日
I've got batches of data that end in MRDC.number (MRA slices e.g. i3078011.MRDC.205) and I want to order them by filename extensions i.e. MRDC.20,MRDC.21,MRDC.22,MRDC.23
Trouble is sort_nat for example reads the initial numbers not the extension
They go from MRDC.1 to MRDC.200ish ,but may vary
I've attached a typical filename cell array.
Cheers
  1 件のコメント
William
William 2015 年 4 月 24 日
This is a far as I've got
ext=char(zeros(size(filen)));
name=char(zeros(size(filen)));
for i = 1:size(filen)
[~,name(i),ext(i)] = fileparts(char(filen(i)));
end

サインインしてコメントする。

採用された回答

Guillaume
Guillaume 2015 年 4 月 24 日
編集済み: Guillaume 2015 年 4 月 24 日
Building up on Ingrid's answer and your code, this will sort the extensions numerically, regardless of the filename:
ext = regexp(filen, '(?<=\.)\d+$', 'match', 'once');
[~, order] = sort(str2double(ext));
filen = filen(order)
I use a regular expression instead of fileparts so I can get rid of the . as well.

その他の回答 (2 件)

Ingrid
Ingrid 2015 年 4 月 24 日
if all your filenames have the same length (which seem to be the case for the file in attachment) you could do this
fin = cellfun(@(x) x(15:end),filen,'UniformOutput',false);
fin = str2double(fin);
[~,idx] = sort(fin);
sortedFileNames = filen(idx);
  1 件のコメント
Ingrid
Ingrid 2015 年 4 月 24 日
alternativaly, if the size of the filename is not the same for all files you could use this
fin = cellfun(@(x) regexp(x,'\.','split'),filen,'UniformOutput',false);
fin = cellfun(@(x) x{end},fin,'UniformOutput',false);
fin = str2double(fin);
[~,idx] = sort(fin);
sortedFileNames = filen(idx);

サインインしてコメントする。


Stephen23
Stephen23 2015 年 4 月 24 日
編集済み: Stephen23 2015 年 4 月 24 日
There are several natural order sorting functions on MATLAB File Exchange, and they often use file-names in their examples... but they all suffer from the same problem: that extension separator character just keeps getting in the way!
To avoid this problem I created natsortfiles, which sorts the file-names and file-extensions separately. Take a look at the code and you will find it easy to adapt for your purposes.
It never occurred to me that someone might want to sort only by file-extension, but that would be a useful option to add...
Alternatively, because the extensions consist entirely of digits, simply split these from the names and sort them numerically:
>> load('filenames.mat')
>> [pth,nam,ext] = cellfun(@fileparts,filen,'UniformOutput',false);
>> [~,idx] = sort(cellfun(@(s)sscanf(s,'.%i'),ext));
This uses the built-in fileparts function, so it is not depended on the file-name lengths or anything like that, and the sscanf call allows us to neatly ignore the period character. Two lines of code and you have your sort index!

カテゴリ

Help Center および File ExchangeShifting and Sorting Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by