Putting files with numeral string names into numeric order
Mostrar comentarios más antiguos
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 comentarios
Azzi Abdelmalek
el 9 de Jun. de 2013
can you give a sample of data you want to sort
Steven
el 10 de Jun. de 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'
Respuesta aceptada
Más respuestas (1)
Kelly Kearney
el 11 de Jun. de 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 comentarios
Steven
el 11 de Jun. de 2013
Kelly Kearney
el 12 de Jun. de 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
el 12 de Jun. de 2013
Categorías
Más información sobre Characters and Strings en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!