readtable to specify date format to avoid ambiguity on a single variable in file.

126 views (last 30 days)
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.

Answers (1)

Stephen23
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)
Warning: Column headers from the file were modified to make them valid MATLAB identifiers before creating variable names for the table. The original column headers are saved in the VariableDescriptions property.
Set 'VariableNamingRule' to 'preserve' to use the original column headers as table variable names.
sondeData = 19×4 table
Temp Speed ErrorFlag packetTime ______ _____ _________ ________________ 11.88 5.1 0 11/10/2022 13:09 12.09 5.11 0 11/10/2022 13:12 12.31 5.16 0 11/10/2022 13:20 14.3 5.6 0 11/10/2022 13:22 14.515 5.63 0 11/10/2022 13:09 15.263 5.785 0 11/10/2022 13:09 16.011 5.94 0 11/10/2022 13:09 16.759 6.095 0 11/10/2022 13:09 17.507 6.25 0 11/10/2022 13:09 18.255 6.405 0 11/10/2022 13:09 19.003 6.56 0 11/10/2022 13:09 19.751 6.715 0 11/10/2022 13:09 20.499 6.87 0 11/10/2022 13:09 21.247 7.025 0 11/10/2022 13:09 21.995 7.18 0 11/10/2022 13:09 22.743 7.335 0 11/10/2022 13:09
  2 Comments
Richard Nash
Richard Nash on 18 Oct 2022
Even doing this I stil get the warning - (hence mentioning having tried "inputFormat" in my original post.
I had assumed that in explicitly specifying the format it would not still give me the warning - but it appears to still do so.
Is there still something wrong with my code? or does the warning appear anyway?
Stephen23
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.

Sign in to comment.

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!