MATLAB: Save a sequence of images in a new folder

image processingImage Processing Toolboxsave imagessequence of images

I have this code:
miFolder='C:\Users\karina\Documents\MATLAB\11';
if ~isdir(miFolder)
errorMensaje = sprintf('Error: El folder no existe:\n%s', miFolder);
uiwait(warndlg(errorMensaje));
return;
end
filePatron = fullfile(miFolder, '*.jpg');
jpegFil = dir(filePatron);
Resultados='C:\Users\karina\Documents\MATLAB\11\SerieN';
mkdir(Resultados);
for k = 1:10
baseFN = jpegFil(k).name;
fullFN = fullfile(miFolder, baseFN);
I=imread(fullFN);
I2=imcrop(I,[167.5 0.5 660 524]);
imwrite(I2,[Resultados,num2str(k),'.png']);
end
Then what I want is to save the images in the C:\Users\karina\Documents\MATLAB\11\SerieN location but the images of the SerieN are saved in the C:\Users\karina\Documents\MATLAB\11. All the images are saved as SerieN1.png, SerieN2.png and so on, that's ok but I need to those images will be saved in the correct folder. Thank you for your time. Regards.

Best Answer

  • If you're not going to use fullfile(), and use concatenation like you did, you need to add a trailing space to your folder. It's best to use fullfile() which takes care of all that for you.
    baseFileName = sprintf('%d.png', k); % e.g. "1.png"
    fullFileName = fullfile(Resultados, baseFileName); % No need to worry about slashes now!
    imwrite(I2, fullFileName);