using datetime() in retrieving data

조회 수: 1 (최근 30일)
MA
MA 2021년 9월 30일
댓글: MA 2021년 10월 2일
Assuming I am using datetime() to refer to the date and time that some data has accured in.
date=datetime(1999,06,19,15,45,00); %datetime(year,month,day,hour,min,sec)
I am using 'date' in further comparison to retrive some data, now is there any syntax I may use in this that would allow me for example to retrive the data happened in a certain day for all the years, (ex: the data happened in 1st october from all the years we have) or for a certain hour from all days in months in years?
  댓글 수: 2
Steven Lord
Steven Lord 2021년 9월 30일
How is your data stored? If it's in a timetable array you may find a timerange useful. The isbetween function may also be helpful.
Alternately checking using some of the functions in the "Split Dates and Times" section on the Dates and Times page in the documentation may be of use as @Siddharth Bhutiya showed.
MA
MA 2021년 10월 2일
Thank you, the documentation you suggested helped a lot!. I was using different way for comparison but isbetween made it really simple, thanks a lot.

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

채택된 답변

Siddharth Bhutiya
Siddharth Bhutiya 2021년 9월 30일
Once you have created a datetime array, you could use functions like month, day, year, etc to get the corresponding field values from the datetime. Then as KSSV suggested above, you could use those to index and filter your datetimes.
>> d
d =
4×1 datetime array
01-Oct-2020
02-Oct-2021
01-Oct-2019
01-Sep-2018
>> month(d)
ans =
10
10
10
9
>> d(month(d) == 10 & day(d) == 1) % Get 1st October for all years
ans =
2×1 datetime array
01-Oct-2020
01-Oct-2019
If you want to filter your datetimes for values between two points in time then you could also use something like isbetween: https://www.mathworks.com/help/matlab/ref/datetime.isbetween.html
  댓글 수: 1
MA
MA 2021년 10월 2일
thank you that was really helpful, using these functions sorted the issue for me, that was excatly what I needed.

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

추가 답변 (1개)

KSSV
KSSV 2021년 9월 30일
Read about datevec. This will split your date into year, month, day, hour, minute and secobd. From here you can use logial indexing and get the indices.
idx = month == 10 & day == 1 ;
  댓글 수: 1
Stephen23
Stephen23 2021년 9월 30일
Do NOT convert DATETIME objects to the older, less accurate, less versatile, DATEVEC, DATENUM, etc.
Instead use MONTH, DAY, etc directly on the DATETIME object, and then you can use the usual logical comparisons required for your data operations.

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

카테고리

Help CenterFile Exchange에서 Dates and Time에 대해 자세히 알아보기

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by