Plotting data from a table with a datetime in it, monthly records
13 views (last 30 days)
Show older comments
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.
0 Comments
Accepted Answer
KL
on 25 Oct 2017
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
KL
on 27 Oct 2017
Edited: KL
on 27 Oct 2017
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'))
More Answers (0)
See Also
Categories
Find more on Weather and Atmospheric Science 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!