필터 지우기
필터 지우기

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

조회 수: 9 (최근 30일)
Beatriz Sanchez
Beatriz Sanchez 2019년 2월 1일
댓글: Beatriz Sanchez 2019년 2월 7일
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

답변 (2개)

KALYAN ACHARJYA
KALYAN ACHARJYA 2019년 2월 2일
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
Image Analyst
Image Analyst 2019년 2월 7일
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 2019년 2월 7일
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. :-)

댓글을 달려면 로그인하십시오.


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

카테고리

Help CenterFile Exchange에서 Images에 대해 자세히 알아보기

제품


릴리스

R2016a

Community Treasure Hunt

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

Start Hunting!

Translated by