MATLAB Answers

How to use datetime to plot value vs. hour of day, i.e. daily cycle?

120 views (last 30 days)
K E
K E on 21 Jul 2016
Commented: Peter Perkins on 3 Aug 2016
Let's say I have a time series of humidity with datetime time stamps, which are randomly sampled in time over several days though sample time is monotonic. I would like to plot humidity vs. the monotonic hour of day that the measurement was made, so I can see the daily cycle of humidity (high values around 3pm, regardless of the day the data was collected, for example). My first plot below with datetime fails at doing this. My second plot with datenum shows how I want it to look (monotonic hours on x-axis). I want to know if I can make the same plot using datetime - this will tell me whether to bother switching over to datetime, which has some nice features, or keep using datenum in my code.
%%Failed attempt to make plot using datetime
figure('Name', 'Failed Plot Using Datetime');
% Time vector is monotonic but randomly sampled in time
tDays = datetime(2014,6,28) + days(0:1/24:10) + ...
rand(size(days(0:1/24:10)))*1/24;
% Humidity measurement corresponding with time stamp
humidity = rand(1,length(tDays));
% Attempt to convert from days to hours of day
tHours = datetime(tDays, 'Format', 'HH');
plot(tHours, humidity, 'x');
xlabel('Not Monotonic Hours between 0-24');
ylabel('Humidity');
title('Failed Plot Using Datetime');
%%How I want the plot to look
% However this uses datenum and I want to use datetime instead
figure('Name', 'Plot Using Datenum');
tDays = datenum(2014,6,28) + (0:1/24:10) + rand(size(0:1/24:10));
humidity = rand(size(tDays));
% Conversion from days to hours
tHours = (tDays - floor(tDays))*24;
plot(tHours, humidity, 'x');
xlabel('Monotonic Hours between 0-24');
ylabel('Humidity');
title('Desired Plot, But Not Using Datetime');

  0 Comments

Sign in to comment.

Accepted Answer

Sean de Wolski
Sean de Wolski on 25 Jul 2016
Edited: Sean de Wolski on 26 Jul 2016
EDIT
[tHours.Year, tHours.Month, tHours.Day] = deal(0);
plot(tHours,humidity,'x','DatetimeTickFormat','HH')

  5 Comments

Show 2 older comments
K E
K E on 26 Jul 2016
I was struggling with what the deal(0) call is doing. Are my code commented interpretations right?
tDays = datetime(2014,6,28) + days(0:1/24:10) + ...
rand(size(days(0:1/24:10)))*1/24;
humidity = rand(1,length(tDays));
% Put tHours in memory before the deal command
tHours = datetime(tDays, 'Format', 'HH');
% deal(0) sets all values of the Year, Month, and Day fields
% of the datetime object tHours to zeros, so only the Hours
% field is non-zero
[tHours.Year, tHours.Month, tHours.Day] = deal(0);
% This plots montonic hours from 0-24 on x-axis
plot(tHours,humidity,'x','DatetimeTickFormat','HH');
title('Desired Plot Using Datetime');
xlabel('Hours of Day');
ylabel('Humidity');
Sean de Wolski
Sean de Wolski on 26 Jul 2016
Yes, it's simply a one-line approach to
dt.Year = 0;
dt.Month = 0;
dt.Day = 0;
Peter Perkins
Peter Perkins on 3 Aug 2016
This is not a good way to do this. You want to plot vs. durations, created using the timeofday function:
plot(timeofday(tDays),humidity,'o')

Sign in to comment.

More Answers (0)

Sign in to answer this question.

Tags