Rename image file name in matlab

前端 未结 4 609
小蘑菇
小蘑菇 2021-01-14 10:26

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

相关标签:
4条回答
  • 2021-01-14 11:08

    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).

    0 讨论(0)
  • 2021-01-14 11:09

    I got one line shorter

    datafiles = dir('*.jpeg');
    for i = 1:length(datafiles)
        fileOut = strrep(datafiles(i).name, '0000.jpeg', '.jpeg');
        movefile(datafiles(i).name, fileOut);
    end
    
    0 讨论(0)
  • 2021-01-14 11:12

    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.

    0 讨论(0)
  • 2021-01-14 11:20

    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');
    
    0 讨论(0)
提交回复
热议问题