필터 지우기
필터 지우기

histogram of datetimes with customisable bin width

조회 수: 24 (최근 30일)
Sim
Sim 2022년 10월 20일
댓글: Sim 2022년 10월 20일
How to plot a histogram of (differences of) datetimes, with a customizable bin width, such as 1 minute ?
This is my attempt, but something is wrong/missing.....
% input
a = datetime({'2022-05-17 16:07:28.280'
'2022-05-17 16:08:55.968'
'2022-05-17 16:19:26.632'
'2022-05-17 16:21:23.840'
'2022-05-17 16:22:47.540'
'2022-05-17 16:25:01.152'
'2022-05-17 16:26:27.728'
'2022-05-17 16:32:47.192'
'2022-05-17 18:00:01.340'
'2022-05-17 18:05:41.080'
'2022-05-17 18:07:11.248'
'2022-05-17 18:08:59.208'
'2022-05-17 18:12:17.940'
'2022-05-17 18:19:33.520'
'2022-05-17 18:21:10.020'
'2022-05-17 18:22:20.740'
'2022-05-17 18:23:55.900'
'2022-05-17 18:25:05.460'
'2022-05-17 18:29:46.080'
'2022-05-17 20:02:22.160'
'2022-05-17 20:03:38.900'
'2022-05-17 20:05:17.112'
'2022-05-17 20:19:10.072'
'2022-05-17 20:20:38.928'
'2022-05-17 20:21:41.000'
'2022-05-17 20:23:16.260'
'2022-05-17 20:24:27.460'
'2022-05-17 20:28:48.980'});
% histcounts
b = diff(a);
binLimits = [min(b) max(b)];
binWdith = datetime('00:01:00');
numBins = ceil(minutes(diff(binLimits))/minute(binWdith));
[counts,binEdges]=histcounts(b, 'BinLimits',binLimits,'BinWidth',binWdith);
Error using datetime/histcounts
X must be a datetime array.
% histogram
histogram(b,'binEdges',binEdges,'FaceColor','k','EdgeColor','k');

채택된 답변

Star Strider
Star Strider 2022년 10월 20일
After doing a few conversions between datetime and duration to get all the arguments to be what the functions want —
% input
a = datetime({'2022-05-17 16:07:28.280'
'2022-05-17 16:08:55.968'
'2022-05-17 16:19:26.632'
'2022-05-17 16:21:23.840'
'2022-05-17 16:22:47.540'
'2022-05-17 16:25:01.152'
'2022-05-17 16:26:27.728'
'2022-05-17 16:32:47.192'
'2022-05-17 18:00:01.340'
'2022-05-17 18:05:41.080'
'2022-05-17 18:07:11.248'
'2022-05-17 18:08:59.208'
'2022-05-17 18:12:17.940'
'2022-05-17 18:19:33.520'
'2022-05-17 18:21:10.020'
'2022-05-17 18:22:20.740'
'2022-05-17 18:23:55.900'
'2022-05-17 18:25:05.460'
'2022-05-17 18:29:46.080'
'2022-05-17 20:02:22.160'
'2022-05-17 20:03:38.900'
'2022-05-17 20:05:17.112'
'2022-05-17 20:19:10.072'
'2022-05-17 20:20:38.928'
'2022-05-17 20:21:41.000'
'2022-05-17 20:23:16.260'
'2022-05-17 20:24:27.460'
'2022-05-17 20:28:48.980'});
% histcounts
b = diff(a);
dtb = datetime(string(b));
binLimits = [min(dtb) max(dtb)];
binWdith = duration('00:01:00');
numBins = ceil(minutes(diff(binLimits))/minutes(binWdith));
[counts,binEdges]=histcounts(dtb, 'BinLimits',binLimits,'BinWidth',binWdith);
% histogram
histogram(dtb,'binEdges',binEdges,'FaceColor','k','EdgeColor','k');
Is that the desired result?
.
  댓글 수: 3
Cris LaPierre
Cris LaPierre 2022년 10월 20일
Since b is created already using the datetime function, the following step is not necessary
dtb = datetime(string(b));
Sim
Sim 2022년 10월 20일
thanks a lot @Cris LaPierre ! :-)

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Cris LaPierre
Cris LaPierre 2022년 10월 20일
By taking the diff of a, your variable b is now a duration, not a datetime. That means your binWidth must be of the same datatype. Make that a duration, and you code works.
I didn't see a need for numBins, so I removed that line of code.
% input
a = datetime({'2022-05-17 16:07:28.280'
'2022-05-17 16:08:55.968'
'2022-05-17 16:19:26.632'
'2022-05-17 16:21:23.840'
'2022-05-17 16:22:47.540'
'2022-05-17 16:25:01.152'
'2022-05-17 16:26:27.728'
'2022-05-17 16:32:47.192'
'2022-05-17 18:00:01.340'
'2022-05-17 18:05:41.080'
'2022-05-17 18:07:11.248'
'2022-05-17 18:08:59.208'
'2022-05-17 18:12:17.940'
'2022-05-17 18:19:33.520'
'2022-05-17 18:21:10.020'
'2022-05-17 18:22:20.740'
'2022-05-17 18:23:55.900'
'2022-05-17 18:25:05.460'
'2022-05-17 18:29:46.080'
'2022-05-17 20:02:22.160'
'2022-05-17 20:03:38.900'
'2022-05-17 20:05:17.112'
'2022-05-17 20:19:10.072'
'2022-05-17 20:20:38.928'
'2022-05-17 20:21:41.000'
'2022-05-17 20:23:16.260'
'2022-05-17 20:24:27.460'
'2022-05-17 20:28:48.980'});
% histcounts
b = diff(a);
binLimits = [min(b) max(b)];
binWidth = duration('00:01:00');
[counts,binEdges]=histcounts(b, 'BinLimits',binLimits,'BinWidth',binWidth);
% histogram
histogram(b,'binEdges',binEdges,'FaceColor','k','EdgeColor','k');
  댓글 수: 3
Cris LaPierre
Cris LaPierre 2022년 10월 20일
numBins is also just the length of counts.
Sim
Sim 2022년 10월 20일
thanks :)

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Annotations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by