MATLAB: Datetime from csv: format change from 2017b to 2018 and higher

datetimeformat

I have the following code to import data from csv file:
filename = '201810.csv';
scenariodata = readtable(filename, 'delimiter', ',');
Some of the data in the csv file is in date format HH:MM:SS, for example in field (3,8) the data is 3:32:11.
In Matlab 2017b, this field is imported as '3:32:11', whereas in newer versions it becomes just 3:32:11.
As a result, when I try
datetime(scenariodata{3,8},'InputFormat','HH:mm:ss','TimeZone','Asia/Tokyo')
I get the following error message:
"Error using datetime (line 639) Input data must be a numeric array, a string array, a cell array containing character vectors, or a char matrix.".
How should I change my code for newer Matlab versions?
Thank you!

Best Answer

Hi Emma,
I haven't checked between releases but I noticed that the 'readtable' function has a 'DatetimeType' name-value pair option:
'DatetimeType' The output type of datetime variables. The possible values
are:
'datetime' - Return dates as MATLAB datetimes.
'text' - Return dates as text.
'exceldatenum' - Return dates as Excel serial day date numbers.
Therefore I suggest trying:
scenariodata = readtable(filename, 'delimiter', ',', 'DatetimeType', 'text');
Otherwise you could call:
whos scenariodata{3,8}
in the command line to see what data type is being used and handle it accordingly.
HTH,
Ben