Adjusting the numbers in a string
    15 次查看(过去 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!


