How to re-order a string array?

11 ビュー (過去 30 日間)
james Green
james Green 2022 年 4 月 11 日
コメント済み: Paul 2022 年 4 月 11 日
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')

採用された回答

Stephen23
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
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 件)

DGM
DGM 2022 年 4 月 11 日
  1 件のコメント
james Green
james Green 2022 年 4 月 11 日
thanks that works perfectly, however this is part of an uni assignment which i can only subbmit one .m file for, so i dont think i can use this. im not very familiar with file exchange is there a way to get around this.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeShifting and Sorting Matrices についてさらに検索

タグ

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by