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 Center および File ExchangeCalendar についてさらに検索

製品


リリース

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by