MATLAB Answers

How to find the sum of duration if the consecutive row elements are the same?

4 views (last 30 days)
Mehnaz Khan
Mehnaz Khan on 17 Apr 2021
Commented: Mehnaz Khan on 19 Apr 2021
I have a table of datetime Activated and Location
e.g.
'2019-12-08 10:32:26' 'Room 1 '
'2019-12-08 10:33:32' 'Room 2'
'2019-12-08 10:34:35' 'Room 2'
'2019-12-08 10:35:09' ''Room 3'
'2019-12-08 10:36:56' 'Room 3'
'2019-12-08 10:38:24' 'Room 3'
'2019-12-08 10:40:17' 'Room 1'
'2019-12-08 10:43:06' 'Room 1'
'2019-12-08 10:44:28' 'Room 1'
'2019-12-08 10:45:28' 'Room 2'
and I have used diff(table.datetime,1,1) to return a separate duration file of duration spent in each room before the next room is activated
e.g.
'00:01:06'
'00:01:03'
'00:00:34'
'00:01:47'
'00:01:28'
'00:01:53'
'00:02:49'
'00:01:22'
'00:04:02'
'00:07:18'
Now I want to find the duration of time spent in each room, i.e. the start and end time of a repeated train of activations of a single room. E.g. time spent in room 2 would be the duration from 10:33:32 to 10:35:09 (i.e. sum of duration values of same rows in the duration file)
Is there a way to loop through the table and calculate time spent in each room?

Answers (1)

Stephan
Stephan on 17 Apr 2021
Edited: Stephan on 17 Apr 2021
Save the durations you calculated using diff in an additional column so that the durations are correct for every row in your table. I guess the last entry would be NaN or zero or something like that.
Then have a look at groupsummary. This should solve your problem.
  6 Comments
Mehnaz Khan
Mehnaz Khan on 19 Apr 2021
Ok great thank you!
So, the next step from this would be to find the start and end time of repeated activations in the same room?
i.e. room 1 is activated 3 times consecutively from 00:08:24 to 00:25:00
room 2 is then activated 2 times consecutively from 00:25:00 to 00:30:09

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!