Date input from csv changing

6 vues (au cours des 30 derniers jours)
Victoria
Victoria le 25 Mar 2025
Commenté : Victoria le 26 Mar 2025
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

Réponse acceptée

Cris LaPierre
Cris LaPierre le 25 Mar 2025
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);
  1 commentaire
Victoria
Victoria le 26 Mar 2025
This worked thanks!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Dates and Time dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by