readtable to specify date format to avoid ambiguity on a single variable in file.
98 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(1 个)
Stephen23
2022-10-18
编辑:Stephen23
2022-10-18
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 个评论
Stephen23
2022-10-19
"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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Calendar 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!