Date/time matrix for every hour.
22 views (last 30 days)
Show older comments
I want to to create an hourly date/time matrix starting from 2006-12-01 and ending: 2016-12-31. It must start at midnight and end at 23:00 for each day. How do I do that?
Accepted Answer
dpb
on 21 Apr 2017
Edited: dpb
on 21 Apr 2017
Without datetime class, use datenum...same idea, just a little more work
>> first = datenum(2006, 12, 1);
>> last = datenum(2017, 1, 1);
>> xt = datenum(2006, 12, 1,[0:(last-first)*24-1].',0,0);
>> datestr(xt(1))
ans =
01-Dec-2006
>> datestr(xt(end))
ans =
31-Dec-2016 23:00:00
>>
datenums are just doubles so don't have the display features of the specialized class, have to use datestr to show the calendar form.
See
doc datenum % to convert to datenums from string/numeric format
doc datestr % for output calendar formatting
doc datetick % for plot axes
for the details; more examples. Also if plotting, use the datenum as the value but have to fix up the axis display with datetick. There are some other supporting functions; see the "See Also" sections.
Since datenums are just doubles, ":" doesn't have the magical properties it has with the datetime class to use as Steven did in his answer; instead datenum has the internal "smarts" to wrap any field based on calendar time; hence the hours vector from the beginning time. It's tempting to try to do the calculation outside datenum as
dt=1/24; % one hour fraction of day
dn=datenum(start):dt:datenum(end); % generate hourly datenum series
but this is fraught with problems of floating point roundoff, always use an integer series of the proper graunularity as above to ensure comparisons will work as expected.
1 Comment
Chris Hooper
on 13 May 2022
for what it's worth, this correction worked for me:
>> xt = datenum(2006, 12, 1,[0:( 1+ last-first)*24-1].',0,0);
More Answers (2)
Steven Lord
on 20 Apr 2017
Use datetime.
first = datetime(2006, 12, 1);
last = datetime(2016, 12, 31);
x = first:hours(1):last;
You may want to change the display format of the vector x. You can do this by changing the vector's Format property. [That doesn't change how the data is stored, just how it is displayed.]
x.Format = 'dd-MMM-yyyy hh:mm a'; % day-month-year hour:minute AM/PM
sampleFromNearTheMiddle = x(43210:43220)
7 Comments
Peter Perkins
on 21 Apr 2017
Regarding formats: this
>> datetime(2017,1,1)-hours(1)
ans =
datetime
31-Dec-2016 23:00:00
creates the desired format (or at least something that shows time of day). But dpb, you're right, the colon operation does only use the starting point's format, and could be smarter.
But if you know you're working on hourly times, the following slight mod to Steve's original makes the problem go away
>> first = datetime(2006,12,1,0,0,0);
>> last = datetime(2016,12,31,23,0,0);
>> x = first:hours(1):last;
>> x(1:3)
ans =
1×3 datetime array
01-Dec-2006 00:00:00 01-Dec-2006 01:00:00 01-Dec-2006 02:00:00
>> x(end-2:end)
ans =
1×3 datetime array
31-Dec-2016 21:00:00 31-Dec-2016 22:00:00 31-Dec-2016 23:00:00
dpb
on 21 Apr 2017
"...the colon operation does only use the starting point's format,..."
Ah! that's where it is buried in how/where the decision made; having noticed previously sometimes it was, sometimes it wasn't but I hadn't deduced that was the controlling factor in the previous cases owing, probably, to them having come from independent usages rather than multiple tries at the same session that would have revealed the pattern.
Appreciate the info, Peter, makes it make more sense and know what to do/coach about in future both for own use as well as on forum. As we've discussed, the datetime class is pretty dense with features and the most effective usage of same is pretty difficult to prise from the doc's in some instances.
Another thread a day or two ago I thought it would be the cat's meow for but turned out "not so much"--couldn't figure out a syntax to effectively select an index match of a year and arbitrary months from a datetime variable vector; instead was still a lot simpler in use to store year/month independently altho it increases storage as in this particular case were three separate dates-->six year/month pairs.
Ayomide Olabode
on 12 Oct 2019
Could I get help reproducing this image please?
1 Comment
dpb
on 12 Oct 2019
This should be new question; has nothing to do with the thread to which you attached it. Please repost.
See Also
Categories
Find more on Dates and Time 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!