Rename multiple files by substituting specific characters

Hi all,
i have multiple .csv files, ordered and named following this scheme:
file.0.csv, file.1.csv, file.2.csv, etc.
i do have a problem while using:
files = dir('*.csv');
if I give the command:
files.name
I get this wrong order:
file.0.csv, file.1.csv, file.10.csv, file.100.csv, file.101.csv, etc.
I suppose the problem is the dot after "file", so I'd like to rename all files by substituting the dots with something else (underscore for example) to fix the issue.
Anyone can help me?
Thanks in advance

 採用された回答

Andrei Bobrov
Andrei Bobrov 2013 年 12 月 6 日

0 投票

files = dir('*.csv');
[~,ii] = sort(cellfun(@(x)sscanf(x,'file.%d.csv'),{files.name}));
file_sorted = files(ii);

1 件のコメント

EK
EK 2013 年 12 月 6 日
Works awesome, Thank you Andrei

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

その他の回答 (0 件)

カテゴリ

質問済み:

EK
2013 年 12 月 6 日

コメント済み:

EK
2013 年 12 月 6 日

Community Treasure Hunt

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

Start Hunting!

Translated by