Get specific number of timetable rows after specific timedate

조회 수: 4 (최근 30일)
Daniele Lupo
Daniele Lupo 2020년 4월 5일
댓글: Ameer Hamza 2020년 4월 5일
I've a timetable like the following one:
intersectionPoints =
10×1 timetable
Timestamp Value
____________________ ______
01-Feb-2016 00:00:00 1.0848
01-Feb-2016 01:00:00 1.0847
01-Feb-2016 04:00:00 1.0848
02-Feb-2016 14:07:44 1.0914
02-Feb-2016 17:21:36 1.0916
03-Feb-2016 01:49:18 1.0917
03-Feb-2016 07:18:43 1.0919
04-Feb-2016 00:53:20 1.1088
04-Feb-2016 04:18:16 1.1097
04-Feb-2016 21:38:10 1.1199
I also have a datetime object representing a date between the first and last value of my timetable:
checkDate =
datetime
03-Feb-2016 01:49:20
Now I want to retrieve two rows of my table. The row with the timestap that's just before my checkDate and the row just after it. In my case I need to retrieve these two rows:
% just before 03-Feb-2016 01:49:20
03-Feb-2016 01:49:18 1.0917
% just after 03-Feb-2016 01:49:20
03-Feb-2016 07:18:43 1.0919
I've seen that I can use timerange for it, but's not suited for my purposes. I need to set a start datetime and an end timedate for defining a timerange, and I don't know the distance between samples in my timetable, so I can miss values. I can have timetables where time intervals between consecutive rows span between one second and many years (they're result of some preprocessing).
Is there a way to obtain my result without using timerange or some linear search?

채택된 답변

Ameer Hamza
Ameer Hamza 2020년 4월 5일
편집: Ameer Hamza 2020년 4월 5일
Try something like this
% example values
t = datetime('1/1/2020 00:00:00'):hours(8):datetime('31/1/2020 23:59:59');
val = rand(size(t));
t = timetable(t',val');
checkdate = datetime('1/15/2020 09:00:00');
idx = [find(t.Time < checkdate,1,'last') find(t.Time > checkdate,1,'first')];
values = t(idx,:);
  댓글 수: 4
Daniele Lupo
Daniele Lupo 2020년 4월 5일
Much better. This is what I wanted, thanks.
Ameer Hamza
Ameer Hamza 2020년 4월 5일
Glad to be of help.

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

추가 답변 (0개)

카테고리

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

제품


릴리스

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by