Why doesn't Matlab recognize my data format?

Hello everyone,
thanks for reading. My point is that I have a time series from which I want to remove all the 29 of february. My time series is 10 years long.
Now, if I ask Matlab what's the format of my datetime, it replies me with:
ans =
'dd/MM/uuuu'
and, in fact it is correct. But when I ask Matlab to find in my matrix the position of 29 of february I get this error:
Unable to convert the text '29/02/2018' to a datetime value because its
format was not recognized.
Does someone know why?
I specify that I also tried to do table2timetable before running the find function, but it doesn't work.
Thank you!

답변 (2개)

Cris LaPierre
Cris LaPierre 2022년 6월 22일
편집: Cris LaPierre 2022년 6월 22일
The error is because 2018 was not a leap year. 2016 and 2020 were.
d1 = '29/02/2016';
datetime(d1)
ans = datetime
29-Feb-2016
% The error you see
d2 = '29/02/2018';
datetime(d2)
Error using datetime
Could not recognize the date/time format of '29/02/2018'. You can specify a format using the 'InputFormat' parameter. If the date/time text contains day, month, or time zone names in a language
foreign to the 'en_US' locale, those might not be recognized. You can specify a different locale using the 'Locale' parameter.

댓글 수: 2

Jan
Jan 2022년 6월 22일
You have answered this question 30 minutes before?! I did not see your answer before I typed my onw and even not after I sent it. Is it possible that I see the contents with a delay of 30 minutes, when I access the US server of mathworks.com from Europe?
I wouldn't rule anything out. @Rena Berman might be able to answer more definitively.

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

Jan
Jan 2022년 6월 22일
편집: Jan 2022년 6월 22일
a = datetime('28/02/2018', 'InputFormat', 'dd/MM/uuuu')
a = datetime
28-Feb-2018
b = datetime('29/02/2018', 'InputFormat', 'dd/MM/uuuu')
% FAILS:
% Unable to convert the text '29/02/2018' to a datetime value because its
% format was not recognized.
There was no Februrary 29th in 2018.
a + days(1) % 01-Mar-2018
ans = datetime
01-Mar-2018
A workaround might be the old datenum format:
b = datetime(datevec('29/02/2018', 'dd/mm/yyyy'))
a = datetime
01-Mar-2018
Ugly, but smarter.

댓글 수: 1

jjjSAN
jjjSAN 2022년 6월 23일
편집: jjjSAN 2022년 6월 23일
Thank you all. Finally was just this easy thing of leap years fortunately :)
Sorry

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

카테고리

도움말 센터File Exchange에서 Dates and Time에 대해 자세히 알아보기

제품

릴리스

R2022a

질문:

2022년 6월 22일

편집:

2022년 6월 23일

Community Treasure Hunt

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

Start Hunting!

Translated by