Matlab:在结构字段中按大小对字符串进行排序
Matlab: sorting strings on size in a struct field
这个问题困扰着我,解决方案可能很明显,但我找不到。
我有一堆数据文件要加载:
ex_file-1.txt, ex_file-2.txt, ..., ex_file-10.txt
要获取他们的文件名,我使用:
files = dir('ex_file-*.txt');
这个returns一个包含字段名、类型等的结构。字段名returns:
ex_file-1.txt, ex_file-10.txt, ex_file-2.txt, ..., ex_file-9.txt
我想这样排序,ex_file-10.txt
是最后一个文件而不是第二个。
我尝试过连接、转换为单元格并进行排序,但 none 似乎满足了我的需要。我知道最明显的解决方案是重命名所有文件名,以便所有字符串具有相同的长度,但我不想这样做。
这可能是一种方法 -
%// 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'
这可以使用正则表达式来完成。文件名的数字部分被检测为 .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
这是一个不需要任何文件名细节的替代方案。一级排序规则最短在前,二级字典序:
%secondary sorting
list=sort(list);
%primary sorting by length
[a,b]=sort(cellfun(@numel,list)):
list=list(b);
这个问题困扰着我,解决方案可能很明显,但我找不到。
我有一堆数据文件要加载:
ex_file-1.txt, ex_file-2.txt, ..., ex_file-10.txt
要获取他们的文件名,我使用:
files = dir('ex_file-*.txt');
这个returns一个包含字段名、类型等的结构。字段名returns:
ex_file-1.txt, ex_file-10.txt, ex_file-2.txt, ..., ex_file-9.txt
我想这样排序,ex_file-10.txt
是最后一个文件而不是第二个。
我尝试过连接、转换为单元格并进行排序,但 none 似乎满足了我的需要。我知道最明显的解决方案是重命名所有文件名,以便所有字符串具有相同的长度,但我不想这样做。
这可能是一种方法 -
%// 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'
这可以使用正则表达式来完成。文件名的数字部分被检测为 .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
这是一个不需要任何文件名细节的替代方案。一级排序规则最短在前,二级字典序:
%secondary sorting
list=sort(list);
%primary sorting by length
[a,b]=sort(cellfun(@numel,list)):
list=list(b);