Save figure with all subplots

I generated a lot of figures with subplots. I want to save them and don't want to display them.
I realized only the last subplot is saved. Is there anyway I can save all subplots in a figure.
The following is an example code.
t=linspace(1,10,20);
y=2*t;
for i=1:3
f=figure('visible','off');
subplot(1,3,i)
p=plot(t,y);
saveas(f,'test','jpg')
end

1 comentario

Trang Doan
Trang Doan el 19 de Abr. de 2021
Movida: Adam Danz el 7 de Oct. de 2024
how to save 1 figure of the subplots

Iniciar sesión para comentar.

Respuestas (1)

Walter Roberson
Walter Roberson el 9 de Oct. de 2019

2 votos

Construct new file names each iteration.
saveas(f, sprintf('test%d.jpg', i))

4 comentarios

Sharon
Sharon el 9 de Oct. de 2019
Editada: Sharon el 9 de Oct. de 2019
Thank you Walter.
I made the mistake using
figure('visible','off')
So it generates new figure every loop.
After correcting it to
t=linspace(1,10,20);
y=2*t;
for i=1:3
f=figure(1);
set(f,'Visible', 'off')
subplot(1,3,i)
p=plot(t,y);
saveas(f,'test','jpg')
end
It works.
Walter Roberson
Walter Roberson el 9 de Oct. de 2019
That would overwrite test.jpg each iteration.
Sharon
Sharon el 9 de Oct. de 2019
You are right. I should put saveas outside the loop.
Adam Danz
Adam Danz el 7 de Oct. de 2024
I'd also like to point readers in the direction of exportgraphics instead of saveas.
exportgraphics(fig, filename)

Iniciar sesión para comentar.

Etiquetas

Preguntada:

el 9 de Oct. de 2019

Comentada:

el 7 de Oct. de 2024

Community Treasure Hunt

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

Start Hunting!

Translated by