The date format from my readtable is wrong so I get "NaT"

조회 수: 12 (최근 30일)
Juan
Juan 2021년 6월 21일
댓글: Walter Roberson 2022년 7월 25일
I really can't believe this was never asked after I googled for hours.
I have a REALLY long csv file with about 1million rows with dates. The format they were written is 'dd/MM/yyyy hh:mm:ss'.
When I try to import those values I keep getting "NaT" on dates like the 31st of october. It's obvious that MATLAB is trying to read on format 'MM/dd/yyyy' instead of 'dd/MM/yyyy'.
I've seen another posts about people that change formats after they imported the data so it would be really easy, but it's not the case.
I've changed the date format on "Preferences->Command Window' but I keep getting frustrated with 'NaT' on half of the data.
I've thought about two options:
  • changing the format of readtable (which I tried that already doing something like a=readtable(file,'Range','e5:e931478','DatetimeType','dd/mm/yyyy hh:mm'); but it says
"Error using readtable (line 318)
Expected input to match one of these values:
'datetime', 'text', 'exceldatenum'
The input, 'dd/mm/yyyy hh:mm', did not match any of the valid values."
  • The other thing I could try is to import the dates as another format like texst char or strings and THEN try to read them.
I'm really desperate here can anyone help ?
a=readtable(file,'Range','e5:e931478','DatetimeType','dd/mm/yyyy hh:mm');
  댓글 수: 3
Qiurui Zhu
Qiurui Zhu 2022년 7월 25일
But I have not import the data yet. How do I let it recognize the var name in the "setvaropts(var,'InputFormat','dd/MM/yyyy hh:mm')" command. In my case, the error is "Unrecognized function or variable 'TIMESTAMP'."
Walter Roberson
Walter Roberson 2022년 7월 25일
opt = detectImportOptions(file);
opt = setvaropt(opt, 'TIMESTAMP', 'InputFormat', 'dd/MM/yyyy hh:mm');
tData = readtable(file, opt);

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

답변 (0개)

카테고리

Help CenterFile Exchange에서 Calendar에 대해 자세히 알아보기

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by