Plot average monthly flow from Jan-Dec for 'x' number of yrs on one plot using the provided data

2 views (last 30 days)
Hello MATLAB Community,
I want to plot the average monthly values from Jan-Dec for 'x' number of years on the same plot. I was able to calculate the average using groupsummary. How can I use the isbetween or any other function to separate out those years in varaible 'yr' from the Mean table and plot them in single plot. I have also attached the sample output which I am expecting to get.
clc
clear
close all
data = readtable('Test.xlsx');
Mean = groupsummary(data,'Date','month','mean');
yr=[1977,1988,1989,1991,2003]

Accepted Answer

Cris LaPierre
Cris LaPierre on 29 Jul 2022
Group by year and monthofyear. Then you can use ismember and reshape to identify and organize the data in a manner that will allow you to generate the desired plot.
file = 'https://www.mathworks.com/matlabcentral/answers/uploaded_files/1082210/TEST%20(1).xlsx';
data = readtable(file);
Warning: Column headers from the file were modified to make them valid MATLAB identifiers before creating variable names for the table. The original column headers are saved in the VariableDescriptions property.
Set 'VariableNamingRule' to 'preserve' to use the original column headers as table variable names.
Mean = groupsummary(data,{'Date_Time','Date_Time'},{'year','monthofyear'},'mean','WaterLevel')
Mean = 456×4 table
year_Date_Time monthofyear_Date_Time GroupCount mean_WaterLevel ______________ _____________________ __________ _______________ 1977 1 31 870.48 1977 2 28 896.25 1977 3 31 843.29 1977 4 30 864.07 1977 5 31 829.68 1977 6 30 1251 1977 7 31 1425.2 1977 8 31 1345.5 1977 9 30 1124.3 1977 10 31 825.16 1977 11 30 794.27 1977 12 31 813.52 1978 1 31 876.52 1978 2 28 858.93 1978 3 31 806.65 1978 4 30 763.37
yrs = [1977,1988,1989,1991,2003];
yr=ismember(str2double(string(Mean.year_Date_Time)),yrs);
x = reshape(Mean.monthofyear_Date_Time(yr),12,[]);
y = reshape(Mean.mean_WaterLevel(yr),12,[]);
plot(x,y)
xticklabels({'Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'})
legend(string(yrs))

More Answers (0)

Community Treasure Hunt

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

Start Hunting!