Save figure in a subplot loop

조회 수: 3 (최근 30일)
Frederik Reese
Frederik Reese 2022년 5월 4일
편집: Jonas 2022년 5월 4일
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에서 Printing and Saving에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by