Putting files with numeral string names into numeric order
Show older comments
I am trying to sort the order of some experiment files in a directory. The software package that generates the files uses internally generated file names. There are a total of 50 files. The file name comes up something like 'eight00000.T000.D000.P001.H001.CFD.txt. I know that the directory command 'dir' will produce a list of files in the order of 'eight000000','eighteen000000','eleven000000','fifteen000000' and so on. I need to put the files into a numeric order, so 'zero','one','two','three' but be able to reverse the sort order.
I have tried using regexp() and sort () without success.
Any thoughts?
3 Comments
Azzi Abdelmalek
on 9 Jun 2013
can you give a sample of data you want to sort
Steven
on 10 Jun 2013
>> C = {'eight00000.T000.D000.P001.H001.CFD.txt'; 'eighteen.T000.D000.P001.H001.CFD.txt'; 'eleven.T000.D000.P0001.H001.CFD.txt'; 'fifteen.T000.D000.P0001.H001.CFD.txt'; 'five.T000.D000.P0001.H001.CFD.txt'};
>> [~,idx] = sort(cellfun(@words2num,C));
>> C(idx)
ans =
'five.T000.D000.P0001.H001.CFD.txt'
'eight00000.T000.D000.P001.H001.CFD.txt'
'eleven.T000.D000.P0001.H001.CFD.txt'
'fifteen.T000.D000.P0001.H001.CFD.txt'
'eighteen.T000.D000.P001.H001.CFD.txt'
Accepted Answer
More Answers (1)
Kelly Kearney
on 11 Jun 2013
How about something like this?
files = dir('*.txt');
numname = regexp({files.name}, '^[a-z]*', 'match', 'once');
allnum = {'zero', 'one', 'two', 'three'}; % etc
[tf, loc] = ismember(numname, allnum);
[blah, isrt] = sortrows([loc' (1:length(files))']);
{files(isrt).name}'
I don't actually have Matlab running right now so I haven't tested this and it probably has some typos, but the basic idea should work.
3 Comments
Steven
on 11 Jun 2013
Kelly Kearney
on 12 Jun 2013
Just need to alter the regular expression a bit. I also altered the code a bit so it doesn't assume anything about the initial order of files.
files = {...
'eight00000.T000.D000.P001.H001.CFD.txt'
'eighteen.T000.D000.P001.H001.CFD.txt'
'eleven.T000.D000.P0001.H001.CFD.txt'
'fifteen.T000.D000.P0001.H001.CFD.txt'
'five.T000.D000.P0001.H001.CFD.txt'
'twenty-eight000000.T000.D000.P002.H001.CFD.txt'
'twenty-five000000.T000.D000.P002.H001.CFD.txt'
'twenty-four000000.T000.D000.P002.H001.CFD.txt'
'twenty-nine000000.T000.D000.P002.H001.CFD.txt'};
[srt, order1] = sort(files); % sorted alphanumerically
wordnum = {'one', 'two', 'three', 'four', 'five', 'six', 'seven', ...
'eight', 'nine', 'ten', 'eleven', 'twelve', 'thirteen', ...
'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', ...
'nineteen', 'twenty', 'twenty-one', 'twenty-two', ...
'twenty-three', 'twenty-four', 'twenty-five', 'twenty-six', ...
'twenty-seven', 'twenty-eight', 'twenty-nine', 'thirty'};
filewnum = regexp(files, '[A-Za-z\-]*', 'match', 'once');
[tf, loc] = ismember(filewnum, wordnum);
[srt, order2] = sortrows([loc order1]);
sortedfiles = files(order2)
Steven
on 12 Jun 2013
Categories
Find more on Characters and Strings in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!