Plot time-of day histogram for array of datetimes
You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Show older comments
0 votes
Share a link to this question
How do I create a histogram for an array of datetimes showing the counts of times of day from 00:00 to 24:00. I'd like the bin width to be variable between 1 to 60 minutes.
Accepted Answer
dpb
on 1 Oct 2025
0 votes
7 Comments
dormant
on 1 Oct 2025
Sorry, but I don't understand your answer.
I've only recently started using datetime and have no idea what a class is and how to use it.
So, we don't understand exactly what you have to start with, either. Show us a sample of your data set.
As a rough guess,
x=rescale(randn(1000,1),0,24*60*60-1); % a set of seconds
d=duration(0,0,x,'format','hh:mm'); % turn into duration
histogram(d)

or
figure
tl=tiledlayout('flow');
nexttile(tl)
d=datetime(2025,1,1,0,0,x,'format','hh:mm'); % turn into datetime
histogram(d)
d=d-datetime(2025,1,1,'Format','hh:mm'); % convert to duration from beginning of day
nexttile(tl)
histogram(d)

gives back the first
Note that if the datetimes include more than one day, using them directly will bin by day and time, not just time; hence the use of a duration instead. The subtraction of the date from the array of values can be vectorized for all; one doesn't have to have a fixed date.
dormant
on 2 Oct 2025
I've got an array of datetimes spanning a period of 5 years.
>> size( datetimeVTs )
ans =
1 2544
>> datetimeVTs(1:5)
ans =
1×5 datetime array
2020-01-01 07:22:06 2020-01-01 11:35:31 2020-01-02 04:44:33 2020-01-03 12:18:37 2020-01-04 07:19:49
I want to do a histogram that ignores the dates.
dormant
on 2 Oct 2025
It is easier than I thought:
d=timeofday(datetimeVTs);
histogram(d);
dormant
on 2 Oct 2025
Thanks for the help. you pointed me the right way.
dpb
on 2 Oct 2025
No problem -- I should have recalled the timeofday function; subtracting the base datetime for each day is what it does. Seems like maybe when the datetime class was first introduced it wasn't yet there, maybe, is why I fixated on using the duration class; I don't recall precisely. I do remember there being an Answers Q? quite a long time ago about creating some specialized plots that the poster had initially done with datenum required recasting; maybe that recollection got me sidetracked.
Anyways, glad you looked in more depth on your own...
"... have no idea what a class is..."
The object-oriented implementation of data types are classes in MATLAB.
dn=now % get current time as datenum
dn = 7.3989e+05
dt=datetime(dn,'ConvertFrom','datenum') % convert to a datetime variable
dt = datetime
04-Oct-2025 16:41:25
t=timeofday(dt) % and get the time of day corresponding
t = duration
16:41:25
and see what each is in MATLAB
whos
Name Size Bytes Class Attributes
dn 1x1 8 double
dt 1x1 8 datetime
t 1x1 24 duration
and we see each is a different class; they have different properties and methods specific for what they represent and, in the case of the time-related variables, how they represent time. A datenum is "just" a regular floating point double variable interpreted in a particular way; the integer portion represents the days after the origin reference date and the fractional part the fraction of a day of 24 hours.
datestr(dn)
ans = '04-Oct-2025 16:41:25'
dayn=floor(dn);
datestr(dayn,'dd=mmm-yyyy HH:MM:SS')
ans = '04=Oct-2025 00:00:00'
datestr(dn-dayn,'dd=mmm-yyyy HH:MM:SS')
ans = '00=Jan-0000 16:41:25'
By comparison,
dt-dateshift(dt,'start','day')
ans = duration
16:41:25
returns the same time within the day as the fractional port of the datenum or timeofday function.
As a general rule, the use of datetime and duration classes is the better choice; in particular plotting as here is greatly simplified for time-related axes that have to be manually configured when using datenums because, as shown above, there is no indication that the particular variable is any different than any other double for the generation of the axes as displaying user-interpretable times. You can now see the advantage of having a class that does impart that knowledge.
More Answers (0)
Categories
Find more on Simulink in Help Center and File Exchange
See Also
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)