Sorting specific dates from Datetime array
이전 댓글 표시
Hello,
I have a Datetime array (size: 1x1200) in the form of: 'dd-MMM-uuuu HH:mm:ss'
This array is allready orgenized by date & time from oldest sample to newer sample. I.e. Date(1,1) = oldest sample ; Date(1,1200) = Current time/day.
The sampling process is done randomaly. Thus, on a given date I could have a few samples (on differant hours of the day..) or none at all.
I have 2 user inputs: DateStart and DateEnd. Both are text inputs in the form of "dd/MM/uu".
I need to check which samples occured between DateStart to DateEnd (Including those days). DateStart and DateEnd can be the same date.
I tried searching for the specific dates but beacuse my array also shows the time (and I input date only) I get zero results...
% I converted the user input into datetime format.. then:
any(Date=='22-Apr-2020')
% This returns logic 0 result even though this day exist in Date...
% Next, I tried specific date:
any(Date=='22-Apr-2020 03:54:57')
% This works OK and returns logic 1.
So, I tried sorting it by logocal operators:
Smp1 = Date > DateCheckEnd;
Smp2 = Date < DateCheckStart;
SmpTotal = ~(Smp1 | Smp2 )
But this does not work well becuase the dates for DateCheckEnd are not being included.
I'm guessing that my input without hours defults to '22-Apr-2020 00:00:00' and so all the later samples that date being excluded.
I tried by changing to: Date.Format = 'dd-MMM-uuuu'
But this keeps the data of the time. and thus, still does not work.
Any suggestions how to resolve this?
답변 (2개)
Steven Lord
2020년 7월 11일
Let's create some sample time and date data spanning from five hours (300 minutes) ago to four hours (240 minutes) from now.
N = datetime('now');
dt = N + minutes(randi([-300 240], 30, 1));
Which of those took place between three hours ago and two hours from now? If we use the <= and >= operators we can include the end points.
earlyEndpoint = N - hours(3);
laterEndpoint = N + hours(2);
isinrange = (earlyEndpoint <= dt) & (dt <= laterEndpoint);
timesInRange = dt(isinrange);
timesNotInRange = dt(~isinrange);
howFarFromCenterInRange = timesInRange - N;
howFarFromCenterOutOfRange = timesNotInRange - N;
Let's put the results in table arrays for easy display.
allInRange = table(timesInRange, howFarFromCenterInRange)
allOutOfRange = table(timesNotInRange, howFarFromCenterOutOfRange)
댓글 수: 4
Me
2020년 7월 11일
Steven Lord
2020년 7월 11일
The key line is this one:
isinrange = (earlyEndpoint <= dt) & (dt <= laterEndpoint);
A particular element of isinrange will be true if the corresponding element of dt is greater than or equal to the datetime stored in earlyEndpoint AND less than or equal to the datetime stored in laterEndpoint. Then I use that to perform logical indexing into dt.
Me
2020년 7월 13일
Steven Lord
2020년 7월 14일
dateonly = datetime('today')
dateAndTime = datetime('now')
dateAndTime > dateonly % true unless dateAndTime was created at midnight
dateAndTime > (dateonly + days(1))
This is easy using datetime and days:
>> vd = linspace(days(-2),days(2),10);
>> dt = datetime+vd(:)
dt =
12-Jul-2020 00:48:23
12-Jul-2020 11:28:23
12-Jul-2020 22:08:23
13-Jul-2020 08:48:23
13-Jul-2020 19:28:23
14-Jul-2020 06:08:23
14-Jul-2020 16:48:23
15-Jul-2020 03:28:23
15-Jul-2020 14:08:23
16-Jul-2020 00:48:23
>> bd = datetime('2020-07-13');
>> ed = datetime('2020-07-15');
>> ix = dt>=bd & dt<ed+days(1)
ix =
0
0
0
1
1
1
1
1
1
0
카테고리
도움말 센터 및 File Exchange에서 Dates and Time에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!