Datetime won't recognise some cells
显示 更早的评论
I have a series of datetimes in the format 'HH:mm:ss.sss'. I am using the following code to convert from string into datetimes.
data.TimeofDay = datetime(data.TimeofDay,'Format','HH:mm:ss.sss')
In the attached .mat file, it contains the data file before and after the command above. For some reason, different values in the column are returned as NaT despite all rows seemingly being in the same format.
1 个评论
the cyclist
2022-6-28
编辑:the cyclist
2022-7-9
Relevant for both your code and some of the answers:
- 'Format' governs what the output looks like
- 'InputFormat' governs what the input looks like
In your case, I believe MATLAB would have correctly inferred your input format, if had you left it unspecified:
datetime(data.TimeofDay)
采纳的回答
更多回答(2 个)
the cyclist
2022-6-28
编辑:the cyclist
2022-6-28
Two minor but important changes to your syntax:
datetime(data.TimeofDay,'InputFormat','HH:mm:ss.SSS')
Note that I used
- 'InputFormat' instead of 'Format'
- .SSS instead of .sss
load datetime-problem.mat
databackup.TimeofDay
% original Format specification:
datetime(databackup.TimeofDay,'Format','HH:mm:ss.sss')
% corrected Format specification:
datetime(databackup.TimeofDay,'Format','hh:mm:ss.SSS')
类别
在 帮助中心 和 File Exchange 中查找有关 Dates and Time 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!