views:

948

answers:

3

Hi

I Load 10,000 image files from internet site and i save it in the folder to use it in my project (image retrieval system ), now i need to rename image file in a sequential name like (image1,image2,image3,....image10000) , any one can help me... I would like to inform you that I used matlab in my work

thank

+2  A: 

rename a file in matlab:

movefile('myfile.m','myfile2.m')

and you need to get all files in a folder, try something like:

filelist = dir('*.jpg');
Yin Zhu
A: 

You could use matlab's "movefile" function (first parameter is the source name, second parameter is the destination) or when you write the image originally you could specify the file e in your imwrite command.

Either way, I suspect you'll need to loop through a direcory listing, this can be done with either the "dir" or "ls" functions.

Mark E
+6  A: 

One thing you'll want to keep in mind is exactly how the format of the number part of the file name will look, as this can sometimes affect the ordering of the files in the directory. For example, using the naming convention you give above will sometimes result in a sort order like this:

image1.jpg
image10.jpg
image11.jpg
image2.jpg
image3.jpg
...

This isn't generally what you would want. If you instead pad the number with zeroes up to the maximum number size (in your case 5 digits), the sort order should be maintained better in the directory:

image00001.jpg
image00002.jpg
image00003.jpg
....

To create file names like this, you can use the SPRINTF function. Here's some sample code that renames all the .jpg files in a directory in this way:

dirData = dir('*.jpg');         %# Get the selected file data
fileNames = {dirData.name};     %# Create a cell array of file names
for iFile = 1:numel(fileNames)  %# Loop over the file names
  newName = sprintf('image%05d.jpg',iFile);  %# Make the new name
  movefile(fileNames{iFile},newName);        %# Rename the file
end

The above code also uses the DIR and MOVEFILE functions (as mentioned in the other answers).

gnovice
+1 for the working code!
Yin Zhu
Hi Gnovice Thank you so much for your replying, I try this code and I get a good result .. thank you againzenab
zenab
@zenab: Glad to help. Feel free to mark an accepted answer (wink).
gnovice