Plot time-of day histogram for array of datetimes
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
0 개 추천
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
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.
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.
It is easier than I thought:
d=timeofday(datetimeVTs);
histogram(d);
Thanks for the help. you pointed me the right way.
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에 대해 자세히 알아보기
참고 항목
웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 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)
