how to determine if vector of row times is within timerange?

1 view (last 30 days)
I have a vector of row times and a time range:
dates = datetime(2000,1,1):calyears(1):datetime(2002,1,1);
tr = timerange(datetime(2000,1,1), datetime(2001,12,31));
and I would like to determine whether "dates" is within the timerange, such that i get
answer = [true;true;false]
answer = 4×1 logical array
1 1 0 0
Is there a function or approach that does this?
  1 Comment
Stephen23
Stephen23 on 24 Jan 2023
Edited: Stephen23 on 24 Jan 2023
Note that using datetime(2001,12,31) as the bin edge will miss any times that fall during the 31st of December:
isbetween(datetime(2001,12,31,12,35,45), datetime(2000,1,1), datetime(2001,12,31))
ans = logical
0

Sign in to comment.

Accepted Answer

Rik
Rik on 24 Jan 2023
The timerange datatype is really only intended to be used with timetables. Once you convert your dates to a timetable, you can use the containsrange function:
dates = datetime(2000,1,1):calyears(1):datetime(2002,1,1);
tr = timerange(datetime(2000,1,1), datetime(2001,12,31));
tmp = timetable(reshape(dates,[],1),ones(numel(dates),1));
[tf,whichRows] = containsrange(tmp,tr)
tf = logical
1
whichRows = 3×1 logical array
1 1 0

More Answers (0)

Categories

Find more on Dates and Time in Help Center and File Exchange

Tags

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!