lacy
lacy

Reputation: 13

Matlab copy all images to another file in different names

I want to copy all images in a file to another file with different names. But, images order changes during copy. For example, this order like that: B0. jpg,B1.jpg,..,B9.jpg,B10.jpg,B11.jpg..,B30.jpg. I want to change the name of B0.jpg to image1.jpg, B1.jpg to image2.jpg in a similar. But, it changes B0.jpg to B10.jpg and then B11.jpg instead of B1,B2,B3...Because of that images order changed. How can I fix this problem?

Upvotes: 1

Views: 2065

Answers (1)

Rafael Monteiro
Rafael Monteiro

Reputation: 4549

The problem is the SO orders the file names using ASCII sorting, as they're strings (it doesn't look to numbers differently). The string "10" is placed before the string "2", because "1" < "2".

Instead of relying on the order, you could do something like this:

imgs = dir('*.jpg');
for i = 1:numel(imgs)
    % Change the 'B' to 'image'
    newName = strrep(imgs(i).name, 'B', 'image');

    % Copy the image
    copyfile(imgs(i).name, ['c:\destination\' newName]);
end

Upvotes: 1

Related Questions