필터 지우기
필터 지우기

How to add datetime to a column in a table

조회 수: 5 (최근 30일)
Matheus  Pacifici
Matheus Pacifici 2019년 9월 25일
댓글: Siddharth Bhutiya 2019년 10월 2일
I have a table that looks like this:
startTime endTime A B
________________ ________________ _________________ _____________
09-12-2019 21:00 09-12-2019 22:00 10 2
09-12-2019 22:00 09-12-2019 23:00 1 0
09-13-2019 01:00 09-13-2019 02:00 2 0
09-13-2019 05:00 09-13-2019 06:00 1 0
09-13-2019 13:00 09-13-2019 14:00 8 91
09-13-2019 14:00 09-13-2019 15:00 36 190
09-13-2019 15:00 09-13-2019 16:00 6 18
I want to populate it for every hour from the min to max start time
To generate these hours I used t3=min(Table.startTime); t4=max(Table.endTime); t_tot=t3:hours(1):t4
How can I add the missing hours to the table and have a value of 0 for A and B for these hours?
  댓글 수: 1
Siddharth Bhutiya
Siddharth Bhutiya 2019년 10월 2일
Is endTime for each entry always startTime + 1 hour ? If that is the case then you can create a timetable with startTime, A and B and then use retime to resample your time stamps to be hourly. I would suggest using a timetable instead of a table when working with time-stamped data as that would give you access to useful functions specific to time related data.
data = timetable(startTime,A,B);
hourlyData = retime(data,"hourly","fillwithconstant","Constant",0)
ans =
19×2 timetable
startTime A B
____________________ __ ___
12-Sep-2019 21:00:00 10 2
12-Sep-2019 22:00:00 1 0
12-Sep-2019 23:00:00 0 0
13-Sep-2019 00:00:00 0 0
13-Sep-2019 01:00:00 2 0
13-Sep-2019 02:00:00 0 0
13-Sep-2019 03:00:00 0 0
13-Sep-2019 04:00:00 0 0
13-Sep-2019 05:00:00 1 0
13-Sep-2019 06:00:00 0 0
13-Sep-2019 07:00:00 0 0
13-Sep-2019 08:00:00 0 0
13-Sep-2019 09:00:00 0 0
13-Sep-2019 10:00:00 0 0
13-Sep-2019 11:00:00 0 0
13-Sep-2019 12:00:00 0 0
13-Sep-2019 13:00:00 8 91
13-Sep-2019 14:00:00 36 190
13-Sep-2019 15:00:00 6 18
% If you need the endTimes you can add them after this
hourlyData.endTime = hourlyData.startTime + hours(1);

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

Help CenterFile Exchange에서 Tables에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by