Date/time matrix for every hour.
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?
1 Comment
Accepted Answer
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
dpb
on 20 Apr 2017
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...
Steven Lord
on 20 Apr 2017
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.
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...
David du Preez
on 21 Apr 2017
dpb
on 21 Apr 2017
Means an older release before was introduced...have to use date numbers instead...
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
0 votes
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.
Categories
Find more on Calendar 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!