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

32 views (last 30 days)
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 Comments
Qiurui Zhu
Qiurui Zhu on 25 Jul 2022
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 on 25 Jul 2022
opt = detectImportOptions(file);
opt = setvaropt(opt, 'TIMESTAMP', 'InputFormat', 'dd/MM/yyyy hh:mm');
tData = readtable(file, opt);

Sign in to comment.

Answers (0)

Categories

Find more on Dates and Time in Help Center and File Exchange

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!