How can I use loop to produce multiple graphs?
Show older comments
Hello guys! I have a simple problem but I cannot seem to be able to solve it.
I want to produce 11 graphs within a loop, based on the distinction of my Daily Data according to year (11 years 2006-2016, 11 graphs). Here is what I have done so far, which it doesn't work though:
DTindex = datenum(Daily_Data.year);
lim = min(DTindex);
Hindd = DTindex - lim +1;
YEAR = accumarray(Hindd, datenum(Daily_Data.year), [], @mean );
Deaths_allyears = accumarray(Hindd, Daily_Data.Daily_Deaths, [], @sum );
for YEAR=1:11
DTindex = datenum(Daily_Data.month); %Daily_Data.month should focus on each year separately within every loop
lim = min(DTindex);
Hindd = DTindex - lim +1;
MONTH = accumarray(Hindd, datenum(Daily_Data.month), [], @mean );%MONTH should focus on each year separately within every loop
Deaths_permonth = accumarray(Hindd, Daily_Data.Daily_Deaths, [], @sum ); %Deaths_permonth should focus on each year separately within every loop
m = (1:12).';
Monthly_Temp = accumarray(Hindd, Daily_Data.Daily_T, [], @nanmean );%Monthly_Temp should focus on each year separately within every loop
yyaxis left
plot(m,Deaths_permonth)
xlabel('Month'); xlim([1 12]); xticks([1 2 3 4 5 6 7 8 9 10 11 12]);
xticklabels({'Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'});
ylabel('Monthly Deaths')
yyaxis right
plot(m,Monthly_Temp)
ylabel('Temperature (oC)');
grid on;
title({'City-specific distribution of deaths by month and temperature', YEAR});
end
Hopefully I want to produce 1 graph for each year (like the attached, which is accummulative for all years).
I would really appreciate your help.
PS. I'm on R2019a.
Accepted Answer
More Answers (1)
Bob Thompson
on 25 Feb 2021
If you want individual graphs, add the following inside the loop, before the yyaxis command:
...
figure(i)
yyaxis left
...
If you want all of the plots on the same graph, put a 'hold on' command either inside or before the loop that generates the plots.
If you want all of the plots in the same figure, but different subplots, then use the subplots command, and indexing to select the appropriate pane.
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!