How to plot multiple plots in a for loop without overwriting them?

27 views (last 30 days)
Hello all,
I have been striving to plot the irradiance vector and its corresponding impact over the SOC of the battery, but it seems to be writing the same over and over till the end of the loop, for this case I only set up a 2 for loop.
It is as if it will not load the following corresponding value and make up the second plot with the same older value. The problem arise on figure 1 and 2, do you have any workaround for this matter?
This is the code.
%To simulate the 12 months at one click
close all;
clear;
tic %%%%Mido el tiempo de ejecución
load PED_HDW_GEO_5.mat
%%%%%%%%Storage of variables for each iteration%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
month_=zeros(12,1);
Irr_=zeros(12,1);
SOC_=zeros(12,1);
%%%Here I execute the code twice
for month_number=1:2
if (month_number>=1) && (month_number<=2)
Season=1.30; %%C
else
Season=1.20;
end
%%%SIMULINK model
sim('Full_D_.slx');
disp(month_number)
bdclose('Full_PED_FL.slx');
%%%%Data upload
load month.mat
load Irr.mat
load SOC.mat
%%%%%%time base
Irr.time=Irr.time/3600;
T=Irr.time;
%%%
figure(1)
plot(T, SOC.data);
hold on
grid on
xlim([8 32])
ylabel('SOC (%)', 'FontSize',14)
xlabel('Hours (day)', 'FontSize',14)
title('SOC along the year variation', 'FontSize',14)
% % % saveas(gcf,'_SOC_alongYear.png')
figure(2)
plot(T, Irr.data)
hold on
grid on
xlim([8 32])
xlabel('Hours', 'FontSize',14)
ylabel('Irradiance (W·m²)', 'FontSize',14)
title('Irradiance along year', 'FontSize',14)
SOC= (min(SOC))*100;
I=max(Irr);
% % % % All of the accumulated variables in the for loop
month_(month_number)=month_number;
Irr_(month_number)=I;
SOC_(month_number)=SOC;
end
figure (1)
legend('Jan','Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec')
figure (2)
legend('Jan','Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec')

Accepted Answer

Cris LaPierre
Cris LaPierre on 9 Jul 2021
Edited: Cris LaPierre on 9 Jul 2021
Your plotting code looks fine, and your plot has two lines on it, as shown by the legend. If it did not, you would get a warning about ignoring extra legend items.
I would look into your data. Perhaps your January data is not what you think it should be. I would also rearrange your plotting code so that only the code that needs to be run multiple times is included inside the for loop.
for loop = 1:2
T = 8:32;
SOC.data = rand(1,25);
Irr.data = rand(1,25);
figure(1)
plot(T, SOC.data);
hold on
figure(2)
plot(T, Irr.data)
hold on
end
figure (1)
hold off
legend('Jan','Feb')
grid on
xlim([8 32])
ylabel('SOC (%)', 'FontSize',14)
xlabel('Hours (day)', 'FontSize',14)
title('SOC along the year variation', 'FontSize',14)
figure (2)
hold off
legend('Jan','Feb')
grid on
xlim([8 32])
xlabel('Hours', 'FontSize',14)
ylabel('Irradiance (W·m²)', 'FontSize',14)
title('Irradiance along year', 'FontSize',14)

More Answers (0)

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!