Adjusting the numbers in a string

36 views (last 30 days)
Will
Will on 5 Aug 2014
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

Answers (2)

Christopher Berry
Christopher Berry on 5 Aug 2014
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 Comments
Will
Will on 5 Aug 2014
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
Christopher Berry
Christopher Berry on 5 Aug 2014
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?

Sign in to comment.


Geoff Hayes
Geoff Hayes on 5 Aug 2014
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 Comments
Geoff Hayes
Geoff Hayes on 5 Aug 2014
Edited: Geoff Hayes on 5 Aug 2014
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
Christopher Berry on 5 Aug 2014
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...

Sign in to comment.

Categories

Find more on File Operations in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!