dir sorts filenames wrong
2 ビュー (過去 30 日間)
古いコメントを表示
This is how the files are sorted in the (windows)folder (by name):
Thats also how i want them to to be sorted.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/274644/image.png)
This is how matlab sorts them if i use 'dir':
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/274645/image.png)
what can i do?
thanks!
0 件のコメント
採用された回答
Stephen23
2020 年 3 月 2 日
編集済み: Stephen23
2023 年 3 月 13 日
"what can i do?"
If the filenames contain sufficient leading zeros, then split the names using fileparts, then sort the names, e.g.:
S = dir(....);
C = {S.name};
[~,F] = cellfun(@fileparts,C,'uni',0);
[~,X] = sort(F);
C = C(X);
Tested on some sample filenames:
>> C = {'Data2F.Part010.mdf','Data2F.Part003.mdf','Data2F.Part009.mdf','Data2F.mdf'};
>> [~,F] = cellfun(@fileparts,C,'uni',0);
>> [~,X] = sort(F);
>> C = C(X);
>> C{:}
ans = Data2F.mdf
ans = Data2F.Part003.mdf
ans = Data2F.Part009.mdf
ans = Data2F.Part010.mdf
Otherwise you can download my FEX submission natsortfiles and use it to sort the filenames (it splits the file extension for you, and also takes into account any number values in the names):
The submission includes help and HTML documentation with plenty of examples, you would probably need something a bit like this:
S = dir(..);
S = natsortfiles(S);
0 件のコメント
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で File Operations についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!