readtable to specify date format to avoid ambiguity on a single variable in file.
126 views (last 30 days)
Show older comments
Example file attached.
As you can see one of the columns has date time data. This is in the format dd/mm/yyyy - i.e.european.
If I do the following:
FILENAME = 'testData.csv';
testData = readtable(FILENAME);
then matlab rightly points out that 11/10/2022 is an ambigous date - I need to be clear that this is dd/mm/yy for use later.
So I instead try to do the following to specify my dates are in dd/MM/uuuu format
FILENAME = 'SondeData.csv';
opts = detectImportOptions(FILENAME);
opts = setvaropts(opts,"packetTime",'DatetimeFormat','dd/MM/uuuu HH:mm');
sondeData = readtable(FILENAME,opts);
I still get the same error
Warning: The DATETIME data was created using format 'MM/dd/uuuu HH:mm' but also
matched 'dd/MM/uuuu HH:mm'.
To avoid ambiguity, supply a datetime format using SETVAROPTS, e.g.
opts = setvaropts(opts,varname,'InputFormat','MM/dd/uuuu HH:mm');
note that in the code block shared above I have tried swapping 'DatetimeFormat' for 'InputFormat' as the error message suggests to no avail.
I just cannot get my head around manipulating the options files for readtable - especially when you are trying to make a change that applies to just one of the variables you are importing.
0 Comments
Answers (1)
Stephen23
on 18 Oct 2022
Edited: Stephen23
on 18 Oct 2022
As the warning (it is not an error) message shows, set "inputFormat" (not "datetimeFormat"):
FILENAME = 'testData.csv';
opts = detectImportOptions(FILENAME);
opts = setvaropts(opts,"packetTime",'inputFormat','dd/MM/uuuu HH:mm');
sondeData = readtable(FILENAME,opts)
2 Comments
Stephen23
on 19 Oct 2022
"Is there still something wrong with my code?"
You used 'DatetimeFormat' which is explicitly described in the documentation as being the "Display Format". Setting the 'InputFormat' seems to be the more suitable option. Other than that, your code looks okay.
"or does the warning appear anyway?"
No it does not, as my answer clearly shows.
See Also
Categories
Find more on Logical 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!