Adjusting the numbers in a string
38 ビュー (過去 30 日間)
古いコメントを表示
I am trying to change some filenames through matlab (upwards of 3000 at a time). right now the files are saved as
text_#1.txt, txt_#2.txt,... etc.
I would like to change them so that each number is 3 digits. so text_#1.txt would become text_#001.txt.
any suggestions as to how this could be done?
Thanks
0 件のコメント
回答 (2 件)
Christopher Berry
2014 年 8 月 5 日
Will,
You can use sprinf to create the file name strings and %03d to get up to 3 leading zeros. For example
>> sprintf('test_%03d.txt',1)
ans =
test_001.txt
See the documentation for sprintf for more examples
2 件のコメント
Christopher Berry
2014 年 8 月 5 日
Will,
Sure, I think I understand. You want to rename a bunch of already existing files. What system are you on? Unix, Mac, or Windows?
Geoff Hayes
2014 年 8 月 5 日
Will - if you know the number of files, and all are named as you described, then you could do the following
numFiles = 50; % assume only 50 files
for k=1:numFiles
oldFileName = sprintf('text_#%d.txt',k);
newFileName = sprintf('text_#%03d.txt',k);
movefile(oldFileName,newFileName);
end
The above assumes that the code is being run in the directory where the files are located. Try the above and see what happens!
3 件のコメント
Geoff Hayes
2014 年 8 月 5 日
編集済み: Geoff Hayes
2014 年 8 月 5 日
But we are padding the file name with zeros so it should be fine: 001,002,003,…,010,011,…,etc. Unless there are more than 999 files, in which case you should pad with four zeros.
EDIT note that the above code doesn't care how the files are ordered in the browser (explorer, finder, whatever) but just moves (renames) the files according to the pattern of the file name. So text_#1.txt becomes text_#001.txt, text_#2.txt becomes text_#002.txt, etc. There is no dir statement to get the files in the directory...unless that is what you are doing? If so, then you want to extract the number from the file name and use it in the new file name. Something like
files = dir('text_#*.txt');
for k=1:length(files)
oldFileName = files(k).name;
startHashSym = findstr(oldFileName,'#');
startPerSym = findstr(oldFileName,'.');
if ~isempty(startHashSym) && ~isempty(startPerSym)
fileNumber = str2num(oldFileName(startHashSym+1:startPerSym-1));
newFileName = sprintf('text_#%03d.txt',fileNumber);
movefile(oldFileName,newFileName);
end
end
So we just read all files in the directory that match our pattern, and then find the indices of where the hash (#) symbol and period begin, grabbing the number in between the two. We then use this number to create the new file name, and move/rename the file.
Christopher Berry
2014 年 8 月 5 日
Try the following regexprep in the loop operating on the results of dir
regexprep(files.Name,'(\d*)','${sprintf(''%03d'',str2num($1))}')
Assuming there is only one number in the name...
参考
カテゴリ
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!