How can I use loop to produce multiple graphs?

11 vues (au cours des 30 derniers jours)
Daphne PARLIARI
Daphne PARLIARI le 25 Fév 2021
Commenté : Mathieu NOE le 26 Fév 2021
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.

Réponse acceptée

Mathieu NOE
Mathieu NOE le 25 Fév 2021
hello Daphne
I modified a bit your code - see the comments
only issue for me in line :
Monthly_Temp = accumarray(Hindd_ck, Daily_Data_this_year.Daily_T, [], @nanmean );
i could not make this work with "nanmean" so I changed to regular "mean"
hope it helps
Daily_Data = readtable('Daily_Data_Thessaloniki.xlsx');
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 ck=1:numel(YEAR)
ind = find(Hindd == ck); % this will give which rows to select in Daily_Data table for the "ck" year
Daily_Data_this_year = Daily_Data(ind,:); % extracted table for one (ck) single year
DTindex = datenum(Daily_Data_this_year.month); %Daily_Data.month should focus on each year separately within every loop
lim_ck = min(DTindex); % changed name to make sure not to overwritte a variable used before the for loop
Hindd_ck = DTindex - lim_ck +1; % changed name to make sure not to overwritte a variable used before the for loop
MONTH = accumarray(Hindd_ck, datenum(Daily_Data_this_year.month), [], @mean );%MONTH should focus on each year separately within every loop
Deaths_permonth = accumarray(Hindd_ck, Daily_Data_this_year.Daily_Deaths, [], @sum ); %Deaths_permonth should focus on each year separately within every loop
m = (1:12).';
% Monthly_Temp = accumarray(Hindd_ck, Daily_Data_this_year.Daily_T, [], @nanmean );%Monthly_Temp should focus on each year separately within every loop
Monthly_Temp = accumarray(Hindd_ck, Daily_Data_this_year.Daily_T, [], @mean );%Monthly_Temp should focus on each year separately within every loop
yyaxis left
figure(ck),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(ck)});
end
  4 commentaires
Daphne PARLIARI
Daphne PARLIARI le 26 Fév 2021
It works better than fine!
Mathieu I am grateful for your help.
Mathieu NOE
Mathieu NOE le 26 Fév 2021
My pleasure !

Connectez-vous pour commenter.

Plus de réponses (1)

Bob Thompson
Bob Thompson le 25 Fév 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.

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by