Extracting specific time points from two different time vectors

조회 수: 3 (최근 30일)
Sowmya MR
Sowmya MR 2018년 3월 26일
답변: Peter Perkins 2018년 3월 28일
Hi All,
I have two time vectors:
V1 = ['02-Feb-2018 08:23:42' '02-Feb-2018 08:24:42'.... '02-Feb-2018 09:10:42'] and V2 = [' 8:35 AM' ' 8:36 AM'...' 9:01 AM'].
Now, i want to identify the indices of starting (t0) and ending point (tend) of V2 (in this case t0 = ' 8:35 AM' and ' tend = 9:01 AM') in V1 and initialize a vector V3 (of length V1) which contains '1' between t0 and tend, and '0' in other time points. Since v1 and V2 are in different formats, i am not sure how to use datestr here. When i try datestr(v2), i get the following error:
Subscript indices must either be real positive integers or logicals.
Error in formatdate (line 157)
month = char(strrep(month(dtvector(:,2)), '.', '')); %remove period

답변 (2개)

KSSV
KSSV 2018년 3월 26일
Read about datetime and datevec.
  댓글 수: 3
KSSV
KSSV 2018년 3월 26일
Show us the code...
Sowmya MR
Sowmya MR 2018년 3월 26일
V1=['02-Feb-2018 08:23:42'; '02-Feb-2018 08:24:42';'02-Feb-2018 09:10:42'];
V2 = [' 8:35 AM' ;' 8:36 AM';' 9:01 AM'];
V1=datenum(V1);
V2=datenum(V2);
[~,sind]=min(abs(V2(1)-V1));
[~,eind]=min(abs(V2(end)-V1));
This usually works if V2 is same format as V1.

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


Peter Perkins
Peter Perkins 2018년 3월 28일
I suspect you will be happier using datetime and the isbetween function. Actually, it's hard to tell from your description, If all of your timestamps are on the same date, isbetween is probably what you want, though you'll need to take those partial timestamps and add a date to them.
If your timestamps are on different days, and you are selecting them based on their time of day, regardless of date, then you will want to convert V1 to datetimes, V2 to durations, and construct a logical vector based on applying timeofday to the datetimes.
A more concrete example of what you have and what you want would help. Your example code based on datenums seems to imply that V1 and V2 are all on the same date.

카테고리

Help CenterFile Exchange에서 Time Series Events에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by