Azzera filtri
Azzera filtri

How to find is between values for 1min datetime delta

4 visualizzazioni (ultimi 30 giorni)
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 Commento
dpb
dpb il 5 Dic 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.

Accedi per commentare.

Risposta accettata

dpb
dpb il 5 Dic 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 Commenti
jinang patel
jinang patel il 5 Dic 2020
thanks the feedback.I see the issue here. However Im struggling to find where the delta in date is coming from. Some more context on what Im trying to do:
I get t4 as a candidate time ch value '151807' (attached in mat file). In order to compare this with the full timematch2 array, I convert this t4 in tlower as below. Im not sure where the date delta as you highlighted above is coming from. I dont have control over in t4 input value,is there a better way to do the intended comparision? Maybe convert the datetime "timematch2" to a different format?
formatIn = 'HHMMSS';
datenumeber=datenum(t4,formatIn);
tlower = datetime(datenumeber,'ConvertFrom','datenum','TimeZone','Europe/Amsterdam','Format', 'HH:mm:ss');
dpb
dpb il 6 Dic 2020
Modificato: dpb il 6 Dic 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:
  1. 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
  2. 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.

Accedi per commentare.

Più risposte (1)

Steven Lord
Steven Lord il 5 Dic 2020
Your datetime array may not display the date data but it includes date data nonetheless.
d = datetime('now')
d = datetime
05-Dec-2020 19:34:03
d2 = d; d2.Format = 'HH:mm:ss'
d2 = datetime
19:34:03
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))
ans = logical
1

Categorie

Scopri di più su Time Series Objects in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by