MATLAB: Import CSV file using readtable gives wrong date time format.

MATLABreadtable formats datetime matlab

Hi,
I am having a little problem setting the format for a variable when I use read table, it is a similar problem to others posted before, but I couldn't understand the responses posted fully.
here is my csv format (only one line, the date is 5th april! not the 4th of may)
05/04/2019 11:43,-712.6206,-204.0489,22.9026,22.3949,22.3029,22.5494,22.6193
I set the first column and wrote it to a csv file previously using
fprintf(fid, string(datetime('now')));
How can I set the import to be in european format not american, (its displaying wrong when I use the data to plot graphs.
Current code is
Table=readtable(fullfile(path,file))
DATAdate=table2array(Table(:,1));
Not really sure how to use the format arguements.
Thanks!
Steve

Best Answer

Steven, I'm guessing that you are in the UK, and your system is set to US. Just a guess. You only give one line of your file, so it's a little hard to know for sure the bigger picture, and also not sure what release of MATLAB you have.
Using the most recent version, R2019a, consider these two files:
>> t = table({'04/05/2019'; '04/06/2019';'04/12/2019';'04/13/2019'},[1;2;3;4])
t =
4×2 table
Var1 Var2
______________ ____
{'04/05/2019'} 1
{'04/06/2019'} 2
{'04/12/2019'} 3
{'04/13/2019'} 4
>> writetable(t,'test1.csv')
>> writetable(t(1:3,:),'test2.csv')
Assuming all the dates are in the same format, the first file has dates that could ONLY be in April, because there's no 13th month. So readtable gives me this.
>> t1 = readtable('test1.csv');
>> t1.Var1.Format = 'dd-MMM-yyyy'
t1 =
4×2 table
Var1 Var2
___________ ____
05-Apr-2019 1
06-Apr-2019 2
12-Apr-2019 3
13-Apr-2019 4
But the second file is ambiguous - it contains no day number larger than 12. Because I'm in the US, it assumes I want MM/dd, but warns me and suggests a way to avoid the issue.
>> t2 = readtable('test2.csv');
Warning: The DATETIME data was created using format 'MM/dd/uuuu' but also matched 'dd/MM/uuuu'.
To avoid ambiguity, use a format character vector. e.g. '%{MM/dd/uuuu}D'
> In matlab.io.internal.readTextFile>textscanReadData (line 559)
In matlab.io.internal.readTextFile (line 238)
In matlab.io.internal.legacyReadtable (line 42)
In readtable (line 225)
>> t2.Var1.Format = 'dd-MMM-yyyy'
t2 =
3×2 table
Var1 Var2
___________ ____
05-Apr-2019 1
06-Apr-2019 2
12-Apr-2019 3
If my earlier guess was right, perhaps you can change your system settings to match your expectations. Barring that, the simplest way around this is to specify a format:
>> t2 = readtable('test2.csv','Format','%{dd/MM/yyyy}D%f');
>> t2.Var1.Format = 'dd-MMM-yyyy'
t2 =
3×2 table
Var1 Var2
___________ ____
04-May-2019 1
04-Jun-2019 2
04-Dec-2019 3
If your file is complicated, I'd recommend using detectimportoptions, and tweak the output form that to read the file exactly as you intend.
Depending on what release you are using, the above may not behave exactly as shown. But this should get you started.