How to save/print multiples images using a for loop?

10 views (last 30 days)
Hello,
so, I'm loading some files within a for loop and I want to print a image for each iteration of the for loop:
folder= dir ('*.mat')
for id=1:length (folder)
load (folder(id).name) %this load the files one by one so I can create an image for each iteration
for ix= 1:rep
hold on
scatter (vec, x_mean(ix, :, 1),'b','filled')
scatter (vec, x_mean(ix, :, 2),'r', 'filled')
plot (vec, rep_prom1,'b')
plot (vec, rep_prom2, 'r')
hold off
set (gca, 'Xtick', vec, 'Xticklabel', vec)
%the lines above create de image that I want for each file
%whit this I espect to get as many images as files loaded, I use 'strcat' and 'num2str' so I can save the images with differents names for each
print(strcat('Densidades Promedio. ', ' Alpha=' ,num2str(alfa), '. p=', num2str(p), '.'), '-dpng')
end
end
But when I run this code, matlab give me only one image with all of the files on it, and save multiples files with the names saved correctly, but this files are damaged and I can't open them. Any idea how can I do this?
thank you very much beforehand

Answers (2)

KALYAN ACHARJYA
KALYAN ACHARJYA on 2 Feb 2019
As you mentioned that you have already read the images using for loop.
Syntax code as follows
for k=1:k %k be numbers of images in the folder
%supoose you do the computation on images one by one and each iteration you need to save the image
%say result image name as result1
destination='D:\Kalyan Works\Matlab Work\od_region_grow_DRIONS\im'; %Complete path to save images
%here 'od_region_grow_DRIONS' folder name and im initial file name of save images in that folder
%save images as im1.png, im2.png etc
imwrite(result1,[destination,num2str(k),'.png']);
end
  3 Comments
Image Analyst
Image Analyst on 7 Feb 2019
You said "print" - do you want to have your code print them immediately on paper in a printer?
You also said plot. What does it mean to "plot" an image (like a graph)??? Do you mean "display"?
Do you want to
  1. plot
  2. print
  3. display
  4. save to disk
the image?
Beatriz Sanchez
Beatriz Sanchez on 7 Feb 2019
thank you very much for your answer. I solved my problem, I think I didn't explain it too well. I wanted to:
  1. load files
  2. plot each file
  3. print each image on a .png file with different names
It turns out that one can't use dot '.' on the funtion 'print'. Remove the dots and add 'figure (id)' before the second for loop solved the error. :-)

Sign in to comment.


Image Analyst
Image Analyst on 2 Feb 2019
See my attached demo. Adapt as needed (like to get rid of the part where I make the plots into a movie.

Categories

Find more on Images in Help Center and File Exchange

Products


Release

R2016a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!