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?

3 次查看(过去 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.

采纳的回答

JohnGalt
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
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.

类别

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