Date input from csv changing
18 views (last 30 days)
Show older comments
Victoria
on 25 Mar 2025 at 18:09
Commented: Victoria
on 26 Mar 2025 at 9:38
Hi,
I'm mew to Matlab and i'm having an issue with date formats. I'm reading from a csv file, but the field of dates is going from 01/11/2012 in the csv to 01/11/0012 in matlab. I've played around with datetime, datestr and others but I can't figure out why it's reading as 0012 in the first place. The csv column is in the Date format and matlab gives me a warning saying it's being read as MM/dd/uuuu (see attached pic)
Is there a way I can get the readtable to interperate the date field as text? Then I can manually assign characters to dd/mm/yyyy?
Eventually, this will be the first of many csv's (all in the same format) being uploaded so I want to avoid changing the csv if possible.
Al the dates are actually the same for this file.
mytable = readtable(string(temp1_file));
dates = unique(mytable.Date)
dates =
datetime
01/11/0012
0 Comments
Accepted Answer
Cris LaPierre
on 25 Mar 2025 at 18:27
The warning is telling you that the datetime format detected in the file is ambiguous. You can avoid this by specifying the format instead. You haven't shared what the data in your csv file looks like, but I'm guessing the dates are captured as 01/11/12
If so, try this.
opts = detectImportOptions(string(temp1_file));
opts = setvaropts(opts,'Date','InputFormat','dd/MM/yy');
mytable = readtable(string(temp1_file),opts);
More Answers (0)
See Also
Categories
Find more on Dates and Time in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!