这个问题困扰着我,解决方案可能很明显,但我找不到。
我有一堆我想加载的数据文件:
ex_file-1.txt, ex_file-2.txt, ..., ex_file-10.txt
要获取我使用的文件名:
files = dir('ex_file-*.txt');
返回带有字段名称,类型等的结构。字段名称返回:
ex_file-1.txt, ex_file-10.txt, ex_file-2.txt, ..., ex_file-9.txt
我想对此进行排序,ex_file-10.txt
是最后一个文件而不是第二个文件。
我试图连接,转换为单元格并排序,但似乎没有提供我需要的东西。我知道最明显的解决方案是重命名所有文件名,以便所有字符串具有相同的长度,但我不想这样做。
答案 0 :(得分:2)
这可能是一种方法 -
%// Input cell array of filenames
names = {'ex_file-1.txt', 'ex_file-10.txt', 'ex_file-2.txt', 'ex_file-3.txt', ...
'ex_file-4.txt', 'ex_file-5.txt'}
%// Reomove the starting common "ex_file" string
stripped_names = strrep(names,'ex_file-','')
%// Remove the ending extension part
stripped_names = strrep(stripped_names,'.txt','')
%// Convert to doubles and then get the sorted indices
[~,idx] = sort(str2double(stripped_names))
%// Use sorted indices to rearrange names array, for the final output
names_out = names(idx)
代码运行 -
>> names
names =
'ex_file-1.txt' 'ex_file-10.txt' 'ex_file-2.txt' 'ex_file-3.txt' 'ex_file-4.txt' 'ex_file-5.txt'
>> names_out
names_out =
'ex_file-1.txt' 'ex_file-2.txt' 'ex_file-3.txt' 'ex_file-4.txt' 'ex_file-5.txt' 'ex_file-10.txt'
答案 1 :(得分:2)
这可以使用正则表达式完成。文件名的数字部分被检测为.txt
部分之前的数字字符的子序列。
files = dir('ex_file-*.txt'); %// get file struct array
names = {files.name}; %// get file names. Cell array of strings
numbers = regexp(names, '\d+(?=\.txt)', 'match'); %// strings with numeric part of name
numbers = str2double([numbers{:}]); %// convert from strings to numbers
[~, ind] = sort(numbers); %// sort those numbers
names_sorted = names(ind); %// apply that order to file names
答案 2 :(得分:1)
这是一个替代方案,不需要任何有关文件名的详细信息。主要排序规则最短,次要词典:
%secondary sorting
list=sort(list);
%primary sorting by length
[a,b]=sort(cellfun(@numel,list)):
list=list(b);