ana
ana

Reputation: 23

Saving image using MATLAB

I made a program in MATLAB to generate images with different specifications, but each time I change one of theses specifications I had to re-save the image under a different name and path. So, I made a for loop to change these specifications, but I don't know how I can make MATLAB save the generated image with different names and different paths...

How can I write a program to make MATLAB save multiple generated images with different names and different paths as a part of for–loop?

Upvotes: 2

Views: 5020

Answers (3)

Miriam Marling
Miriam Marling

Reputation: 1

Since I wanted to save the plots in my loop in a specific folder in my present working directory (pwd), I modified my naming routine as follows:

for s = 1:10
    for i = 1:10 
    <loop commands>
    end
end

% prints stimuli to folder created for them
file_name=sprintf('%s/eb_imgs/%0.3f.tif',pwd,s(i)); % pwd = path of present working  
                                                    % directory and s(i) = the value 
                                                    % of the changing variable
                                                    % that I wanted to document

file_name = /Users/Miriam/Documents/PSYC/eb_imgs/0.700.tif % how my filename appears
print('-dtiff', '-r300', file_name); % this saves my file to my desired location

Upvotes: 0

Rody Oldenhuis
Rody Oldenhuis

Reputation: 38032

If you want to save JPEG, PNG etc., then see @AGS's post. If you want to save FIG files, use

hgsave(gcf, file_name)

instead of the imwrite line. There's also

print('-djpeg', file_name)  %# for JPEG file (lossy)
print('-dpng', file_name)   %# for PNG file (lossless)

as an alternative for imwrite.

Upvotes: 2

AGS
AGS

Reputation: 14498

Put something like this at the end of your loop:

for i = 1:n
  <your loop code>
  file_name=sprintf('%d.jpg',i);  % assuming you are saving image as a .jpg
  imwrite(your_image, file_name);  % or something like this, however you choose to save your image
end

Upvotes: 5

Related Questions