How to convert date and time present in same cell to a matrix having all values in elongated form like Y, M, D, H, Mi, S?
1 次查看(过去 30 天)
显示 更早的评论
I have an excel file of time and want to convert the time values to datevec format. The format of time in excel file is like '1/17/2018 16:20:37', '1/17/2018 16:25:37' and so on. I want these values in this format: year, month, date, hour, minutes and seconds. Also, when I read the xlsx file in MATLAB, it automatically converts time to 12 h format like 4:20:37 pm. Please help.
0 个评论
采纳的回答
JohnGalt
2018-10-1
datevec(datestr('1/17/2018 16:20:37','dd/mm/yyyy HH:MM:SS'))
and more generally
dts = {'1/17/2018 16:20:37', '1/17/2018 16:25:37'}
datevec(datestr(dts,'dd/mm/yyyy HH:MM:SS'))
更多回答(1 个)
Peter Perkins
2018-10-1
I'm gonna suggest that you likely don't need to do this. Unless you are using a pretty old version of MATLAB, use datetime instead of datenum/datevec/datestr.
If you want, for example, the hour field, it's simple to get it from a datetime when you need it:
>> t = datetime({'1/17/2018 16:20:37', '1/17/2018 16:25:37'})
t =
1×2 datetime array
17-Jan-2018 16:20:37 17-Jan-2018 16:25:37
>> t.Hour
ans =
16 16
Also, you should probably be using readtable to get the timestamps and whatever else is in the spreadsheet into MATLAB. It will make life much easier.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Dates and Time 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!