Rename Images and save them with the new name and incrementing each 20

72 views Asked by At

I have 1000 jpg files with file names:1_nb_1_0.0164032559841871, 1_nb_1_20.0611820686609, and I want to rename those 1000 with new names by incrementing two characters such as rename to this name:1_nb_1_20.0611820686609 but incrementing 20 for the new other images.

datafiles = dir('*.jpeg');
for i = 1:length(datafiles)
    fileOut = strrep(datafiles(i).name, '1_nb_1_0.0164032559841871.jpeg', '.jpeg');
    movefile(datafiles(i).name, fileOut);
end
`
1

There are 1 answers

0
Mar On

From what I can understand, what you are trying to do in:

fileOut = strrep(datafiles(i).name, '1_nb_1_0.0164032559841871.jpeg', '.jpeg');

is replace'1_nb_1_0.0164032559841871.jpeg' with the new name? And the new names are from 20 to the end in jumps of 20? If that is the case you can just put it like this:

fileOut = strrep(datafiles(i).name, '1_nb_1_0.0164032559841871.jpeg', strcat(num2str(20*i),'.jpeg'));

And this creates the names 20.jpg,40.jpg,60.jpg... If what you want is just do this within another name like the one you already have it can also be done like this:

fileOut = strrep(datafiles(i).name, '1_nb_1_0.0164032559841871.jpeg', strcat('1_nb_1_',num2str(20*i),'.0164032559841871.jpeg'));

and then you will get 1_nb_1_20.0611820686609.jpg,1_nb_1_40.0611820686609.jpg,... I am not sure if this is what you wanted. If you wanna start with 0 instead of 20 you can change it with num2str(20*(i-1)) or starting with 40 with num2str(20*(i+1))