Search code examples
matlabfilenamesbatch-processingbatch-rename

Batch Change File Names in Matlab


I have 200 JPEG images numbered from 1200 to 1399. How do I change their names from 1200.jpg-1400.jpg to 1.jpg-200.jpg, while keeping the original order of the image names?


Solution

  • This is a problem which can be tackled more efficiently in bash or other shell scripting languages. Probably, it can even be solved just via a single shell find.

    Anyway, matlab can also do the job.

    Consider this.

    list = dir('./stack*.png'); % # assuming the file names are like stack1200.jpg
    
    offset = -1200;  # % numbering offset amount
    
    for idx = 1:length(list)   % # we go through every file name
    
        name = list(idx).name;   
        number = sscanf(name,'stack%f.png',1);   % # we extract the number
        movefile(name,['stack' num2str(number + offset) '.png' ]);   % # we rename the file.
    
    end