Save figure in a subplot loop

4 views (last 30 days)
Frederik Reese
Frederik Reese on 4 May 2022
Edited: Jonas on 4 May 2022
Hi,
i have this code an want to save the figure as png and fig , if there are 5 subplots.
The name should be then for example: hours_5, hours_10....
for i=1:100
if rem(i-1, 5) == 0
figure;
end
sgtitle('WSPL zeitlicher Verlauf Modell D Teil 2')
a(i)=subplot(5, 1, rem(i-1, 5)+1)
hold on;
[maxHQextrem,indexHQextrem]=max(Hoehe_HQextrem(:,i)- Hoehe_Z(:,i));
[maxHQ5000,indexHQ5000]=max(Hoehe_HQ5000(:,i)- Hoehe_Z(:,i));
[maxHQ10000,indexHQ10000]=max(Hoehe_HQ10000(:,i)- Hoehe_Z(:,i));
str = ['Maximaler Überlauf [m]: HQextrem ',num2str(maxHQextrem),' HQ5000 ',num2str(maxHQ5000),' HQ10000 ',num2str(maxHQ10000)];
b(i) = annotation('textbox','String',str,'Position',a(i).Position,'Vert','bottom','FitBoxToText','on');
hold on
% annotation('textbox', 'String',str)
p1=plot (Distanz_Z(:,i) , [Hoehe_Z(:,i), Hoehe_HQextrem(:,i), Hoehe_HQ5000(:,i), Hoehe_HQ10000(:,i)]);
title(['Zeit [h] ', num2str(i)])
grid on
xline(indexHQextrem,'-','Maximum','LabelOrientation','horizontal')
xline(indexHQ5000)
xline(indexHQ10000)
leg=legend(p1,{'Geländehöhe','HQextrem','HQ5000', 'HQ10000'})
title(leg,'WSPL')
newcolors = {'#000000','#7E2F8E','#0000FF','#00FFFF'};
colororder(newcolors)
xlabel('FKM [km]');
ylabel('Höhe über NN [m]')
xticks([738.98 1734.44 2744.03 3687.56 4722.17 5762.52 6831.7 7228.84])
xticklabels({'118','117','116','115', '114', '113', '112', '111'})
end
thanks in advance

Accepted Answer

Voss
Voss on 4 May 2022
for i=1:100
if rem(i-1, 5) == 0
f = figure;
end
sgtitle('WSPL zeitlicher Verlauf Modell D Teil 2');
a(i)=subplot(5, 1, rem(i-1, 5)+1);
% ...
% ... your plotting code
% ...
if rem(i-1, 5) == 4
saveas(f,sprintf('hours_%d.png',i));
saveas(f,sprintf('hours_%d.fig',i));
end
end
  4 Comments
Jonas
Jonas on 4 May 2022
Edited: Jonas on 4 May 2022
use the print function or exportgraphics to control the resolution of the image
Voss
Voss on 4 May 2022
To maximize, you can use the 'WindowState' property of the figure if your version of MATLAB supports it. Otherwise, there are functions on the File Exchange you can use.

Sign in to comment.

More Answers (0)

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!