Save figure in a subplot loop

7 次查看(过去 30 天)
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

采纳的回答

Voss
Voss 2022-5-4
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 个评论
Jonas
Jonas 2022-5-4
编辑:Jonas 2022-5-4
use the print function or exportgraphics to control the resolution of the image
Voss
Voss 2022-5-4
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.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 2-D and 3-D Plots 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by