How to split the time interval (format-duration) column based on specific intervals
조회 수: 1 (최근 30일)
이전 댓글 표시
I am working on a data set that has 1 year worth of data, in which I have a time interval column, and the format of which is duration.
I now want to split that time column into two intervals for further data analysis. Mainly, 1. 06:30-13:30 2. 13:30-21:30
Find a sample of time interval attached.
댓글 수: 4
Stephen23
2018년 5월 26일
편집: Stephen23
2018년 5월 26일
@Paolo: it was a pure mistake on my behalf, for which I apologize. I mistakenly thought the question was yours originally, copied your answer to a comment, and realized too late that it wasn't, at which point I tried to go back using the browser... which do not really work. I am very sorry for that. Your answer was fine: please recreate it (I did not want to do this as you will get the credits if it is accepted). If you are unable to recreate it I will ask TMW to do this on Monday.
Paolo
2018년 5월 27일
편집: Paolo
2018년 5월 27일
@Stephen Its all good, thank you for the explanation. I can appreciate mistakes can happen! I understand you were just trying to look after the community. I was just confused as to what had happened. As I have the code open on Matlab, I will complete another answer like the first one, and will append the .csv file too ;)
채택된 답변
Paolo
2018년 5월 27일
Hi Abdul,
The following code reads from a .csv file and splits the column as you requested in two different arrays, Morning and Evening. You can find the .csv file, times.csv, attached to this answer.
The data in the .csv file:
Date
04:00:00
05:00:00
06:30:00
07:00:00
09:00:00
10:00:00
12:00:00
14:00:00
16:00:00
18:00:00
21:00:00
22:00:00
23:00:00
The code:
%Read data.
a = readtable('times.csv');
data = a.x___Date;
%Convert data to datetime.
all = datetime(cell2mat(data),'Format','HH:mm:ss');
%Define boundaries.
limits = {'06:30:00','13:30:30','21:30:00'};
limits = datetime(limits,'Format','HH:mm:ss');
%Get morning and evening data.
Morning = all(all>limits(1)&all<limits(2));
Evening = all(all>limits(2)&all<limits(3));
There are three boundaries as you specified in the problem. Lower boundary being '06:30:00', middle boundary being '13:30:30' and upper boundary being '21:30:00'.
Morning will contain:
07:00:00
09:00:00
10:00:00
12:00:00
Evening will contain:
14:00:00
16:00:00
18:00:00
21:00:00
댓글 수: 0
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Spreadsheets에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!