How to save a figure through each run of a for loop?

조회 수: 308 (최근 30일)
NH
NH 2013년 3월 23일
댓글: AK 2021년 5월 11일
Hello,
I am running a for loop:
for i=1:5
and in each loop, I am creating and saving a plot:
h=figure
plot(...)
saveas(h,'FIG','png');
end
This is problematic because 'FIG.png' is overwritten each time the for loop runs. How do I make it so that it saves 'FIG1.png','FIG2.png','FIG3.png', etc? In other words, how do I save FIG(i).png?
Thank you for your time.

채택된 답변

Azzi Abdelmalek
Azzi Abdelmalek 2013년 3월 23일
편집: Azzi Abdelmalek 2013년 3월 23일
for k=1:5
h=figure
plot(...)
saveas(h,sprintf('FIG%d.png',k)); % will create FIG1, FIG2,...
end
  댓글 수: 7
Abbey Kirkman
Abbey Kirkman 2021년 2월 19일
hi is there away to save all these graphs to one pdf
AK
AK 2021년 5월 11일
hi, has anyone been able to answer @Abbey Kirkman question about saving all created graphs on one pdf?

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

추가 답변 (3개)

Image Analyst
Image Analyst 2020년 8월 2일
If you have r2020a or later, try exportgraphics().
  댓글 수: 2
serena dsouza
serena dsouza 2020년 8월 2일
I don't have it. I am working on r2016a. Is there any other solution??
Sudheer Bhimireddy
Sudheer Bhimireddy 2020년 8월 4일
편집: Sudheer Bhimireddy 2020년 8월 4일
You can try this,
for i=1:3
.
.
for k=1:5
h=figure;
plot(..);
fig_name = strcat('FIG_',num2str(i),'_',num2str(k));
print(h,fig_name,'-dpng','-r400');
end
end
This way you can print to your desired format and resolution.

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


serena dsouza
serena dsouza 2020년 8월 2일
Hi, I am having same type of code but i want to save figures from two for loop.how can I do this.I have tried by following command but it is giving error.Thank you
for i=1:3
.
.
for k=1:5
h=figure
plot(...)
saveas(h,sprintf('FIG%d%d.png',ik));
end
end

Image Analyst
Image Analyst 2020년 8월 2일

카테고리

Help CenterFile Exchange에서 Printing and Saving에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by