Date/time matrix for every hour.
23 次查看(过去 30 天)
显示 更早的评论
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?
采纳的回答
dpb
2017-4-21
编辑:dpb
2017-4-21
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 个评论
Chris Hooper
2022-5-13
for what it's worth, this correction worked for me:
>> xt = datenum(2006, 12, 1,[0:( 1+ last-first)*24-1].',0,0);
更多回答(2 个)
Steven Lord
2017-4-20
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 个评论
Peter Perkins
2017-4-21
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
2017-4-21
"...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
2019-10-12
Could I get help reproducing this image please?
1 个评论
dpb
2019-10-12
This should be new question; has nothing to do with the thread to which you attached it. Please repost.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Dates and Time 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!