필터 지우기
필터 지우기

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