Plotting data from a table with a datetime in it, monthly records

I want to plot monthly records for my weather station. My weather station outputs a csv file that looks like this:
"Timestamp","Outdoor Temperature","Outdoor Humidity","Dew Point","Heat Index","Wind Chill","Barometric Pressure","Rain","Wind Speed","Wind Average","Peak Wind","Wind Direction","Indoor Temperature","Indoor Humidity"
"8/7/2016 8:12:00 PM","75.7","74","66","77","76","29.85481","27.41","0","0","3.728227","180","83.4","53"
"8/7/2016 8:24:00 PM","74.8","71","65","76","75","29.88434","27.41","0","0","3.728227","180","84","55"
"8/7/2016 8:36:00 PM","73.9","71","63","75","74","29.88434","27.41","0","0","1.864114","202.5","84.2","54"
"8/7/2016 8:48:00 PM","73.2","72","63","74","73","29.88434","27.41","0","0","1.242742","180","84.2","52"
"8/7/2016 9:00:00 PM","72.3","73","62","73","72","1012","27.41","0","0","0","180","84.2","52"
"8/7/2016 9:12:00 PM","71.6","74","62","73","72","1012","27.41","0","0","0","157.5","84","52"
"8/7/2016 9:24:00 PM","70.9","76","63","72","71","1013","27.41","0","0","0","180","84.2","52"
My current code is as follows:
%%Graph daily, monthly weather maps, and show records for each month
weather = xlsread('acuriteweather.xlsx');
getdate = readtable('acuriteweather.csv');
% %%Change table values to categorical array
getdate.Timestamp = categorical(getdate.Timestamp);
% Pull out relevant variables to be graphed, daily and monthly
date_array = datetime(cellstr(getdate.Timestamp),'InputFormat','MM/dd/yyyy HH:mm');
temperature = weather(:,1);
RH = weather(:,2);
dew_point = weather(:,3);
for date_array.Month == 12
maxtemp = max(temperature);
avgtemp = mean(temperature);
mintemp = min(temperature);
maxRH = max(RH);
averageRH = mean(RH);
minRH = min(RH);
maxDP = max(dew_point);
avgDP = mean(dew_point);
minDP = min(dew_point);
end
% Plot monthly records
monthly = [maxtemp avgtemp mintemp; maxDP avgDP minDP; maxRH averageRH minRH];
values = {'Temperature ºF', 'Dew Point ºF', 'Relative Humidity'};
bar(monthly)
set(gca, 'YGrid', 'on', 'XGrid', 'off')
yticks([-10 -5 0 5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 90 95 100])
title('Records for Month')
set(gca,'xticklabel',values)
I am only able to get my variables for all of my data, which spans August 2016 to September 2017. I cannot figure out how to get the maximum, average, and minimum for each month for respectively.

 Accepted Answer

Which version are you using? If it's 2016b or later, please use a timetable. You seemed to be trying to calculate monthly mean, min and max which is similar to the question here. Please take a look at my answer there. link: https://de.mathworks.com/matlabcentral/answers/362919-plot-max-min-mean-of-a-value-in-a-matrix

2 Comments

That worked great! Thank you! Do you happen to know how to make my x axis recognize that my 2016 data starts in August? When I select 2016 to be my year, it displays the axis starting in January since it is only labeled as 1 on the axis. My code now reads:
clear all
weather = readtable('acuriteweather.csv');
weather.dt = datetime(weather{:,1});
weather_TT = timetable(weather.dt,weather{:,2},weather{:,3},weather{:,4}...
,weather{:,5},weather{:,6},weather{:,7},weather{:,8},weather{:,9}...
,weather{:,10},weather{:,11},weather{:,12},weather{:,13},weather{:,14});
weather_TT.Properties.VariableNames = {'OutdoorTemperature'...
'OutdoorHumidity' 'DewPoint' 'HeatIndex' 'WindChill'...
'BarometricPressure' 'Rain' 'WindSpeed' 'WindAverage' 'PeakWind'...
'WindDirection' 'IndoorTemperature' 'IndoorHumidity'};
pick_year = input('Pick a year [ex: 2016]: ','s');
switch pick_year
case '2016'
weather_monthly_mean= retime(weather_TT(weather_TT.Time.Year==2016,:),'monthly','mean');
weather_monthly_max = retime(weather_TT(weather_TT.Time.Year==2016,:),'monthly','max');
weather_monthly_min = retime(weather_TT(weather_TT.Time.Year==2016,:),'monthly','min');
case '2017'
weather_monthly_mean= retime(weather_TT(weather_TT.Time.Year==2017,:),'monthly','mean');
weather_monthly_max = retime(weather_TT(weather_TT.Time.Year==2017,:),'monthly','max');
weather_monthly_min = retime(weather_TT(weather_TT.Time.Year==2017,:),'monthly','min');
end
figure(1)
bar(weather_monthly_max.OutdoorTemperature)
hold on
bar(weather_monthly_mean.OutdoorTemperature)
bar(weather_monthly_min.OutdoorTemperature)
hold off
legend({'max','mean','min'})
set(gca, 'YGrid', 'on', 'XGrid', 'off')
title('Monthly Maximum, Average, Minimum for 2017')
xticklabels({'January','February','March','April','May','June','July','August','September','October','November','December'})
ylabel('Temperature ºF')
yticks([-10 -5 0 5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 90 95 100])
ytickformat('degrees')
xlabel('Month')
For example, this should start with August, not January. For 2017 this is no problem because my data starts from the beginning of the year.
Hi,
I have seen your code and you could probably improve it a bit. Take a look below. (I have copied the sample data from your question to a txt file)
data = readtable('new.txt');
%next line is needed only because your numeric data are strings, why?!
data2 = array2table(str2double(table2array(data(:,2:end))));
TT = table2timetable(data2,'RowTimes',datetime(data{:,1}));
TT.Properties.VariableNames = data.Properties.VariableNames(2:end);
%read the pick_year as a number!
pick_year = input('Pick a year [ex: 2016]: ');
%no switch is needed
TT_monthly_mean= retime(TT(TT.Time.Year==pick_year ,:),'monthly','mean')
TT_monthly_max = retime(TT(TT.Time.Year==pick_year ,:),'monthly','max')
TT_monthly_min = retime(TT(TT.Time.Year==pick_year ,:),'monthly','min')
figure(1)
bar(TT_monthly_max.OutdoorTemperature)
hold on
bar(TT_monthly_mean.OutdoorTemperature)
bar(TT_monthly_min.OutdoorTemperature)
%now, get the months from the data you have, no hard coding needed!
xticklabels(month(TT_monthly_mean.Time,'name'))

Sign in to comment.

More Answers (0)

Categories

Find more on Weather and Atmospheric Science in Help Center and File Exchange

Asked:

on 25 Oct 2017

Edited:

KL
on 27 Oct 2017

Community Treasure Hunt

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

Start Hunting!