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

304 Ansichten (letzte 30 Tage)
NH
NH am 23 Mär. 2013
Kommentiert: AK am 11 Mai 2021
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.

Akzeptierte Antwort

Azzi Abdelmalek
Azzi Abdelmalek am 23 Mär. 2013
Bearbeitet: Azzi Abdelmalek am 23 Mär. 2013
for k=1:5
h=figure
plot(...)
saveas(h,sprintf('FIG%d.png',k)); % will create FIG1, FIG2,...
end
  7 Kommentare
Abbey Kirkman
Abbey Kirkman am 19 Feb. 2021
hi is there away to save all these graphs to one pdf
AK
AK am 11 Mai 2021
hi, has anyone been able to answer @Abbey Kirkman question about saving all created graphs on one pdf?

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (3)

Image Analyst
Image Analyst am 2 Aug. 2020
If you have r2020a or later, try exportgraphics().
  2 Kommentare
serena dsouza
serena dsouza am 2 Aug. 2020
I don't have it. I am working on r2016a. Is there any other solution??
Sudheer Bhimireddy
Sudheer Bhimireddy am 4 Aug. 2020
Bearbeitet: Sudheer Bhimireddy am 4 Aug. 2020
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.

Melden Sie sich an, um zu kommentieren.


serena dsouza
serena dsouza am 2 Aug. 2020
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 am 2 Aug. 2020

Kategorien

Mehr zu Printing and Saving finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by