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
'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
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 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.
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.


Community Treasure Hunt

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

Start Hunting!