Make histogram for array of datetimes

조회 수: 10 (최근 30일)
dormant
dormant 2024년 5월 16일
댓글: dormant 2024년 5월 16일
I have a datetime array:
trigDatetime =
1273×1 datetime array
2024-04-30 01:15:34
2024-04-30 01:19:27
2024-04-30 02:20:49
2024-04-30 03:48:50
2024-04-30 03:49:49
How can I calculate the frequency of occurence between two dates for a bin width of an integer number of hours (1, 2, 3, 4, 6 or 12)?

채택된 답변

Steven Lord
Steven Lord 2024년 5월 16일
Let's make some sample dates and times.
n = 10;
T = datetime('today');
d = T + ...
hours(randi([-24, 24], n, 1)) + ...
minutes(randi(60, n, 1)) + ...
seconds(randi(60, n, 1))
d = 10x1 datetime array
15-May-2024 13:09:38 16-May-2024 01:12:13 16-May-2024 21:27:33 15-May-2024 17:34:09 15-May-2024 12:54:39 16-May-2024 18:39:16 16-May-2024 20:18:13 16-May-2024 21:40:51 15-May-2024 14:15:30 16-May-2024 09:22:37
Now make a histogram.
histogram(d, BinWidth = hours(1))
By default this isn't going to show enough ticks on the X axis for my taste. This adds a few more.
xticks(T + hours(-24:4:24))
If you just want the counts, not the pictures, see histcounts.

추가 답변 (1개)

William Rose
William Rose 2024년 5월 16일
@dormant, I am switching my comment to be an answer which is what I had intended.
First, let's create an array of 1273 random date times in the first week of April:
dt0=datetime(['1-Apr-2024']); % start time
D=duration(0,0,60*60*24*7*rand(1273,1)); % 1273 random durations up to 1 week
dt=dt0+D; % random date times startng at dt0
fprintf ('Datetime range: %s to %s.\n',min(dt),max(dt))
Datetime range: 01-Apr-2024 00:06:04 to 07-Apr-2024 23:58:53.
Now make a histogram with 12 hour bin width
histogram(dt,BinWidth=duration(12,0,0))
You can adjust for bin width=1, 3, 6 hours. Good luck.

카테고리

Help CenterFile Exchange에서 Data Distribution Plots에 대해 자세히 알아보기

제품


릴리스

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by