rename file extension from 1.png to 001.png and so on

hi everyone,can I know how to rename the extension of 1.png to 001.png in matlab script!for your information, from the loop of my coding, the file has been created as 1.png and saved in a folder. however, I want to name the file as 001.png in my matlab script. how to do it.besides, if I have more than one file I should make it automatically rename as 001.png, 002.png ,003.png. how to make it? thank you.

 Accepted Answer

folder='D:\answers\'
for k=1:1
file1=sprintf('%d.png ',k)
file2=sprintf('00%d.png',k)
dos_com=['!rename ', folder file1 file2]
eval(dos_com)
end
Edited code (Simon's suggestions)
folder='D:\answers\' % e.g
for k=1:4
file1=[folder sprintf('%d.png ',k)]
file2=[folder sprintf('00%d.png',k)]
movefile(file1 ,file2]
end

23 Comments

Matlab's movefile command is faster than creating a DOS call. If you create a DOS call, the eval is not needed:
system(['rename ', folder file1 file2]);
hi mr azzi abdelmalek.thanks for answering my question.however, that coding just print out the extension of filename(in command window), not rename the extension of filename in folder.in my coding , I used imwrite to save the file in folder.how can I rename 1.png to 001.png in the same folder.
for example I save the file in a file name like this
filename = strcat ('C:\folder',num2str(k),'.png'); imwrite(im,filename)
the file will be save as 1.png in a folder How can I save it as 001.png automatically?
filename = strcat ('C:\folder','00',num2str(k),'.png')
yes,it defininetly works!thanks in advance mr azzi!
Simon, I was, unsuccessfully, looking for functions like movefile, also ignoring system command Thank you for the informations
No, the file will not saved as "C:\folder\1.png", but as "C:\folder1.png".
Walter's suggest solve your question already,
filename = strcat ('C:\folder\','00',num2str(k),'.png')
I think he corrected it
yea, I'd corrected it!
Mr Azzi, the coding u gave was correct, however how to overcome the overwritten file?
folder='D:\answers\' % e.g for k=1:4 file1=[folder sprintf('%d.png ',k)] file2=[folder sprintf('00%d.png',k)] movefile(file1 ,file2] end dear Simon, the code can be used to moved from one file to another file. however, it still overwritten the previous file .how can I save the previous file and save the next file in the same folder.how to overcome the overwritten problem when saving a file?
Use copyfile() instead of movefile()
yes, it just copy from one file to another file. however,how to run program so that it does not overwrite the previous image.say, I run apple shape so that it will be save as 001.png. then, I run orange shape it will be saved as 002.png and so on.means that the file will save that file and not overwrite the previous image in same folder.how to do that.
Use exist() to test if the file already exists.
this error produces Undefined function 'existfile' for input arguments of type 'char'.
still...it still overwrite the previous image
Please show your code including the existence test.
for k=1:BlobsNo
subImage = rgb2gray(subImage);
subImage = imresize (subImage,[40 40]);
imshow(subImage);
ip=subImage;
filename1=strcat('C:\Users\','00',num2str(k),'.png');%
imwrite( ip,filename1)
exist(file2)
end
based on the code, ip file will be write as 40x40 dimension and save as 001.png in filename1 folder. when I run another image it overwrite 001.png as another image and not save it as 001.png for 1st image, 002.png for 2nd image, 003.png for 3rd respectively when the debug the code for other image..
You haven't written a test to see if the file already exists: you've gone ahead and written a file, and then you test for information about a different file but you do not do anything useful with that information!
if exist(filename1, 'file')
Also, each time through, you are writing out exactly the same information. Is that really your intention ?
the file still exits in file2 folder.when I try to run other image, it will save as 001.png and not 002.pnng. meaning it overwritten the previous image in that folder.
Have you moved the existence test to before you imwrite() ? Are you doing something meaningful when you find that the file exists?
And are you truly wanting to output exactly the same output file for BlobsNo's worth of files ? Notice that once you have converted the image to grayscale when k=1, and resized to 40 x 40, that subImage will remain grayscale and 40 x 40 for all the rest of the values of k.
the save filed should be not overwritten when I tried run with other image.this is the proble. pls help.i am not really good in build matlab syntax

Sign in to comment.

More Answers (1)

Also,
sprintf('%03d.png', FileNumber)
to get the leading zeros

1 Comment

As will the traditional C-like way:
baseFileName = sprintf('%3.3d.png', FileNumber);

Sign in to comment.

Categories

Find more on Share and Distribute Software in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!