Plot time-of day histogram for array of datetimes
現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
古いコメントを表示
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.
採用された回答
dpb
2025 年 10 月 1 日
7 件のコメント
dormant
2025 年 10 月 1 日
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
2025 年 10 月 2 日
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
2025 年 10 月 2 日
It is easier than I thought:
d=timeofday(datetimeVTs);
histogram(d);
dormant
2025 年 10 月 2 日
Thanks for the help. you pointed me the right way.
dpb
2025 年 10 月 2 日
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.
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Simulink についてさらに検索
参考
Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- 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)
