Sorting files in struct according to time in filename
2 ビュー (過去 30 日間)
古いコメントを表示
Hello,
I have jpg images with names:
t_=_15_s.jpg
t_=_30_s.jpg
...
t_=_900_s.jpg
and I read them using dir(*.jpg) command. Unfortunatelly, they are sorted wrongly (they start with t_=_105_s.jpg), and I would like to correct it.
I would like to ask you for help.
BR
Mathew
回答 (1 件)
Stephen23
2024 年 3 月 21 日
編集済み: Stephen23
2024 年 3 月 21 日
You could download my FEX submission NATSORTFILES():
And use it like this:
S = dir('*.jpg');
S = natsortfiles(S);
Or use sufficient leading zeros in the filenames.
4 件のコメント
Voss
2024 年 3 月 21 日
If you can't use natsortfiles, here is an alternative:
unzip images
S = dir('*.jpg');
The original order:
{S.name}.'
Sort:
[~,idx] = sort(str2double(regexp({S.name},'\d+','match','once')));
S = S(idx);
Now they are sorted:
{S.name}.'
Stephen23
2024 年 3 月 21 日
"I can't install these extensions to my Matlab, but thank you for help."
MATLAB files do not need to be installed: if you can write your own file then you can use this code too.
Just unzip the ZIP-file into the current directory and use it.
参考
カテゴリ
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!