How to extract time (HH:mm) from datetime from a table and plot it in interval range?

I have a table with datetime columns and cells and numbers too. The datetime are in default format. I want to extract only the hours from the datetime, and plot it with the column of class double. So, y axis to have the "double" column and x "datetime" but x axis to have intervals such as 00:00-02:00, 02:00-04:00,.....14:00-16:00...22:00-00:00. There are a lot of dates,and what I am trying to do is analyse them in HH:mm rather than chronologically by year and full datetime format. Thanks

 Accepted Answer

Note that the format of the datetime array is irrelevant. It is only used for display. You could of course change the Format so that it only shows hours and minutes but calculations would still involve the date as well.
You use the hour and minute functions to extract the hours and minutes from a datetime array. Alternatively, you use the Hour and Minute properties of the datetime object.
d = datetime + hours(1:10)' + minutes(1:10)'; %create demo datetime a array
hour(d) %get the hour
d.Hour %another way
minute(d) %get the minute
d.Minute %another way
As for the plot, I'm unclear what you want to do. If two values have the same hours and minutes but different day, do you want to plot them at the same abscissa, like a scatter plot?

4 Comments

I am choosing to plot two columns from the table. The datetime column has lots of dates/times like 2015-02-23 17:23:00 . I want to put an interval like, independent of the date, but only the hours. So, the rows for the double (numbers) column that correspond to 16:00-18:00 to be ploted x,y axis where on x axis you have intervevals by 2 hours , from 00:00 till 23:59. And in the first interval to correspond to the values that are in that range, and plotted on the y axis. So let say I gave value of 5 then 10 then 3 then 15 that correspond to the time of 00:00-02:00 (that will be plotted on x axis followed by 02:00-04:00,04:00-06:00 etc..) , to be plotted (scattered or similar) . I hope you understand what I am trying to do. If still not, I will upload some data to clarify it better. Thanks!
Okay that was helpful though. I got the hour from the datetimes, then as a variable I plotted in with the other column to correspond and put the interval using set(gca,'xTick',...) and voila! got what I wanted. If there is more elegant way, please let me know. THanks
Still not very clear what it is exactly you are doing but glad you solved it. It does sound like you want to clump your data by two hours interval. If that is so, then as Peter said, the discretize function would be the easiest way to do it.
+ found this in the archive which helped a lot. hour_decimal = t.Hour+t.Minute/60; plot(hour_decimal,data) now I have the plot I needed. Thanks a lot btw!

Sign in to comment.

More Answers (1)

I'm not entirely clear what you are trying to do in this plot. It sounds like you want to plot things against time of day, not against absolute time. In recent versions of MATLAB, you can do that just as
plot(timeofday(t.TimeStamp),t.Y)
But then you mention something that sounds like you want to discretize the data into 2hr bins. I'm not sure how that would work in a plot, but the discretize function might be some help.

Products

Community Treasure Hunt

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

Start Hunting!