필터 지우기
필터 지우기

Adjusting the numbers in a string

조회 수: 25 (최근 30일)
Will
Will 2014년 8월 5일
댓글: Christopher Berry 2014년 8월 5일
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개)

Christopher Berry
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
Will
Will 2014년 8월 5일
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 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
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
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
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 CenterFile Exchange에서 Tables에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by