Date/time matrix for every hour.

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?

1 Comment

Have a read here and here. It will greatly improve your chances of getting an answer.
What is it exactly that you want? do you need it in the datevector format? Or in some string form?

Sign in to comment.

 Accepted Answer

dpb
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

for what it's worth, this correction worked for me:
>> xt = datenum(2006, 12, 1,[0:( 1+ last-first)*24-1].',0,0);

Sign in to comment.

More Answers (2)

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

Almost, Steven! :)
>> x(end-10:end).'
ans =
30-Dec-2016 14:00
30-Dec-2016 15:00
30-Dec-2016 16:00
30-Dec-2016 17:00
30-Dec-2016 18:00
30-Dec-2016 19:00
30-Dec-2016 20:00
30-Dec-2016 21:00
30-Dec-2016 22:00
30-Dec-2016 23:00
31-Dec-2016 00:00
>>
End condition stops at beginning of the last day, not until 11 PM of the last day as requested...easy enough to fix, of course--
last = datetime(2016, 12, 31)+hours(23); % add the 23 hours
or
last = datetime(2017,1,1)-hours(1); % one short of next day
>> x = first:hours(1):last;
>> x.Format='dd-MMM-yyyy HH:mm'; % this is annoying it reverts
>> x(end-10:end).'
ans =
31-Dec-2016 13:00
31-Dec-2016 14:00
31-Dec-2016 15:00
31-Dec-2016 16:00
31-Dec-2016 17:00
31-Dec-2016 18:00
31-Dec-2016 19:00
31-Dec-2016 20:00
31-Dec-2016 21:00
31-Dec-2016 22:00
31-Dec-2016 23:00
>>
Now it includes hours of the last day as well...
You are correct. Good catch.
About your comment "% this is annoying it reverts" -- on the previous line you overwrote the variable x that existed before. The new variable named x doesn't retain any information about the old variable named x; if it did that would be a bug.
Note that overwriting the whole variable is different from modifying a piece of it; if the Format changed when you executed x(1) = datetime('now') or something similar, that would be a different story.
So what's annoying is not that it reverts, it's that the default Format for a new datetime array isn't what you would like. But as of release R2016a, you can change the default Format in the Command Window preferences.
dpb
dpb on 20 Apr 2017
Edited: dpb on 20 Apr 2017
Yeah, I understand, it just seems like a silly choice for default to throw away the time info if/when there is fractional values in the array.(*)
I'm limited to 32-bit by current hardware so I've gone about as far as I can go for the moment, at least...
(*) And, yes, "throw away" is in regards to output format display as opposed to internal storage...
Hi. Thanks for your answers, I have tired them but when I enter the first line:
first = datetime(2006, 12, 01);
I get an error of :Undefined function 'datetime' for input arguments of type 'double'.
How do I resolve this ?
Means an older release before was introduced...have to use date numbers instead...
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
"...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.

Sign in to comment.

Ayomide Olabode
Ayomide Olabode on 12 Oct 2019
Could I get help reproducing this image please? 1.png

1 Comment

This should be new question; has nothing to do with the thread to which you attached it. Please repost.

Sign in to comment.

Asked:

on 20 Apr 2017

Commented:

on 13 May 2022

Community Treasure Hunt

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

Start Hunting!