How to find is between values for 1min datetime delta
4 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Im trying to find indices from a datetime vector that are between certain time values. The delta between tupper and tlower is 1min
tlower=15:18:07
tupper= tlower+seconds(60)
= 15:19:07
tf = isbetween(timematch2,tlower,tupper);
However inthis case tf is a null vector and doest find the in between indices. Does it only work if duration delta in hrs?
%% the below case works %
tf=isbetween(timematch2,tlower,timematch2(end-1));
EDIT: Attached mat files instead of snippet
1 Kommentar
dpb
am 5 Dez. 2020
tf = isbetween(timematch2,tlower,tupper)
I'll note the code snippet is using timematch2 not timematch that is mentioned. We don't know what the variable was the image is and can't do anything with images, anyway.
Attach a section of an array as .mat file that you think exhibits the symptom.
Akzeptierte Antwort
dpb
am 5 Dez. 2020
>> tlower.Format='Default';
>> tupper.Format='Default';
>> [tlower tupper]
ans =
1×2 datetime array
01-Jan-2020 15:18:07 01-Jan-2020 15:19:07
>> timematch2.Format='Default';
>> [min(timematch2) max(timematch2)]
ans =
1×2 datetime array
02-Dec-2020 00:20:01 02-Dec-2020 23:58:59
>>
Your matching time range is not within the range of times in your vector -- as Steven L notes, a datetime value ALWAYS has an associated date with it whether it is displayed or not; there' no such thing as a datetime that is only a time; that is a duration.
The datetime object is not at all like an old datenum in its design nor its behavior in that way; there is no similar ability to drop the integer portion of a datenum and have the time of day remaining as the fractional portion of a 24-hr day.
2 Kommentare
dpb
am 6 Dez. 2020
Bearbeitet: dpb
am 6 Dez. 2020
"... I'm not sure where the date delta as you highlighted above is coming from."
It's coming from datenum when you create the datenumber --
>> datestr(datenum('151807','HHMMSS'))
ans =
'01-Jan-2020 15:18:07'
>>
datenum also supplies default date information when it is missing; in its case the default is Jan 1 of the current year.
If you don't rely on the internals of the datetime function to infer the input format of the string but specify it and don't override the default formatting for a datetime variable as:
>> tIn=datetime('151807','InputFormat','HHmmSS')
tIn =
datetime
06-Dec-2020 15:18:00
>>
then you would have seen the problem right off the bat.
As shown, datetime also supplies a default date--there is a year/date in the value of the variable; do NOT confuse the display of the variable with the value; while the value doesn't change, what you see isn't necessarily "the rest of the story".
Also note that datetime uses the current date at the time of conversion as the default instead of the first day of the current year.
The way to solve the problem is one of two choices:
- Use the timeofday function instead in which case the date portion is not compared, but there are potential issues of midnight and if you would ever want to go from one portion of a day into the morning of the next; or
- Set the date for the data in the conversion to known value. This could either be the date of the actual data if known from some other source or arbitrary if not, but consistent between the looked-for and target values. This is also somewhat iffy unless your data have some way to identify the timestamp that belongs with it if you ever will have data from different days at one point in time or want to cross day boundaries.
We don't have sufficient knowledge of the overall collection process or purpose to know what else to recommend, but that's the source of the issue.
From what is known, my first inclination would be to use the second example above but add an arbitrary but known date to the conversion so fix the reference time. Ideally, this would be a value that you could retrieve somehow from the input data.
Weitere Antworten (1)
Steven Lord
am 5 Dez. 2020
Your datetime array may not display the date data but it includes date data nonetheless.
d = datetime('now')
d2 = d; d2.Format = 'HH:mm:ss'
If you want to know if the datetime value is between certain times of day, use timeofday. You need to be a bit careful if your time range includes midnight.
isbetween(timeofday(d2), duration(12, 0, 0), duration(22, 0, 0))
0 Kommentare
Siehe auch
Kategorien
Mehr zu Time Series Objects finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!