How to re-order a string array?
11 次查看(过去 30 天)
显示 更早的评论
currently the files are orderd like this, the value increasing by 30 each time up to 1830:

however, with the code i've used, matlab orders the files like this:

is there any way i can sort the files to match the original?
dirName = pwd;
files = dir( fullfile(dirName,'*.txt') );
files = {files.name}';
files = sortrows(files,'ascend')
0 个评论
采纳的回答
Stephen23
2022-4-11
编辑:Stephen23
2022-4-11
P = pwd;
S = dir(fullfile(P,'*.txt'));
C = {S.name};
[~,X] = sort(str2double(regexp(C,'\d+','match','once')));
C = C(X);
1 个评论
Paul
2022-4-11
Or
C = string{S.name});
[~,X] = sort(double(extractBetween(C,"_",".txt")));
C = C(X);
for those of us who can't get our minds around patterns and regexp.
更多回答(1 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Shifting and Sorting Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!