How can I organize data into monthly means/averages across all years? And then plot it in a bar graph?
Show older comments
Hello, I would like to make a barplot of average monthly number of students, in other words the mean of the total students across all school years. So the x-axis would read August, September, October, etc. and the y axis would be the average number of students in that month across all years.
I am not sure where to start on this, below is just some code for students per year, not related to what I am trying to find.
Table = readtable("Student_data.xlsx"); % Read in data
%Part 3: Assign school year (SY) to each daily observation
SY = zeros(size(Table.month));
for i = 1:height(Table)
if Table.month(i)>=8 % months "greater" than or equal to August (month 8) are in the following schoolyear, so add 1 yr
SY(i) = Table.year(i) + 1;
else
SY(i) = Table.year(i); % months "lesser" than August are in the regular year
end
end
Table.schoolyear = SY(:); % add "schoolyear" as a column in Table
%For each school year, sum up the total students just for that year.
g = findgroups(Table.schoolyear); % "group" the unique school years
students = splitapply(@sum, Table.students, g);% sum the total students of each school year "group"
year = splitapply(@mean, Table.schoolyear, g);
%Make a Barplot for annual total students
bar(year,students);
ylabel("Students");
xlabel("School Year");
title("Annual Student Totals");
% Now make a barplot of monthly mean students
% I am stuck here
1 Comment
Hello Macy,
You could try something like this:
Table = readtable("Student_data.xlsx");
Mat=table2array(Table);
Months=zeros(12,1);
for i=1:12
imonth=Mat(Mat(:,1)==i,4);
Months(i)=sum(imonth);
end
MonthLabel={'Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'};
bar(Months);
ylabel("Students");
xlabel("Month");
title("Monthly Student Totals");
set(gca,'xticklabel',MonthLabel);
Accepted Answer
More Answers (0)
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!
