How to split the time interval (format-duration) column based on specific intervals

2 views (last 30 days)
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 Comments
Stephen23
Stephen23 on 26 May 2018
Edited: Stephen23 on 26 May 2018
@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
Paolo on 27 May 2018
Edited: Paolo on 27 May 2018
@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 ;)

Sign in to comment.

Accepted Answer

Paolo
Paolo on 27 May 2018
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

More Answers (0)

Community Treasure Hunt

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

Start Hunting!