Adjusting the numbers in a string

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

回答(2 个)

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 个评论

my only problem with this is that I have to type out the filename each time. I need it to happen automatically because there are too many files and not all the files have the same text before the numbered ending
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?

请先登录,再进行评论。

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 个评论

this is similar to what I have tried. the problem is that matlab orders the files like this
10,1,11,12,13,14,15,16,17,18,19,20,2,21,22, etc
so this puts the wrong numbers on the files
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.
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...

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 File Operations 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by