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

조회 수: 1 (최근 30일)
Emma
Emma 2019년 10월 17일
댓글: Walter Roberson 2019년 11월 7일
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!

채택된 답변

Ben Cunningham
Ben Cunningham 2019년 10월 17일
편집: Ben Cunningham 2019년 10월 17일
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
  댓글 수: 1
Emma
Emma 2019년 11월 7일
편집: Emma 2019년 11월 7일
Thank you, Ben.
I saw that the variable's type was "duration", not "datetime" upon importing it from the csv file, so according to your suggestion I tried:
readtable(filename, 'delimiter', ',', 'DurationType', 'text')
and got the desired results.
Thank you!

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Peter Perkins
Peter Perkins 2019년 10월 30일
readtable now reads timestamps as datetimes by default. I think you have two options:
1) If you are already getting datetimes in your table, there's no need to call datetime to convert text to datetime. Just delete that line.
2) As Ben says, you can turn off the "datetime by default" with DatetimeType.
Your example code is converting one element of a table. I hope you aren't actualloy doing it that way: you should have been able to do it with something like this:
scenariodata.TimeVariableName = datetime(scenariodata.TimeVariabeName,'InputFormat','HH:mm:ss','TimeZone','Asia/Tokyo')
  댓글 수: 2
Emma
Emma 2019년 11월 7일
Thank you, Peter.
Indeed, I should probably change the rest of the code to use datetimes directly, though it seemed the variables were durations once I looked into them.
Walter Roberson
Walter Roberson 2019년 11월 7일
Add the duration column to the datetime column to get absolute times in datetime format.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Dates and Time에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by