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?

2 visualizzazioni (ultimi 30 giorni)
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.

Risposta accettata

JohnGalt
JohnGalt il 1 Ott 2018
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'))

Più risposte (1)

Peter Perkins
Peter Perkins il 1 Ott 2018
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.

Categorie

Scopri di più su Dates and Time in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by