Plot time-of day histogram for array of datetimes

28 Ansichten (letzte 30 Tage)
dormant
dormant am 1 Okt. 2025 um 18:46
Bearbeitet: dpb am 5 Okt. 2025 um 15:17
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.

Akzeptierte Antwort

dpb
dpb am 1 Okt. 2025 um 19:01
With datetimes, use the duration class for times of day..
  7 Kommentare
dpb
dpb am 2 Okt. 2025 um 15:35
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...
dpb
dpb am 4 Okt. 2025 um 14:04
Bearbeitet: dpb am 5 Okt. 2025 um 15:17
"... 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.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Data Distribution Plots finden Sie in Help Center und File Exchange

Produkte


Version

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by