MATLAB Answers

Calculate duration from labeled timestamped data.

2 views (last 30 days)
Hi,
I have a timetable with timestamps and labels of different activities. The stacked plot looks like this.
I would like to calculate the durations of each cycle of the activity. For example, the top plot has 11 "Dump" sequences, so the outcome should be,
Dump = [t1, t2, ........t11], t = duration of each dump instance.
Is there any smarter way to accomplish this without using FOR loop and tracking changes in each row?
I am also attaching the .mat file of the timetable.
Thank you!

  2 Comments

Akira Agata
Akira Agata on 11 Jun 2020
If possible, could you upload your timetable data so that people here can try their idea?
Khandakar Rashid
Khandakar Rashid on 11 Jun 2020
Thank you Akira. I have uploaded the timetable data in the original question as "data.mat".

Sign in to comment.

Accepted Answer

Akira Agata
Akira Agata on 18 Jun 2020
Thank you for providing your data.
I believe the following is an possible solution. I hope this will be somehow helpful for you!
load('data.mat');
% Find start/end time for each 'Dump' period
idx = data.Truck1 == 'Dump';
locStart = find(diff(idx) == 1)+1;
locEnd = find(diff(idx) == -1);
timeStart = data.Time(locStart);
timeEnd = data.Time(locEnd);
% Calculate duration for each 'Dump' period
timeDuration = timeEnd - timeStart;
% Arrange to table
tblTruck1 = table(timeStart,timeEnd,timeDuration);
>> tblTruck1
tblTruck1 =
11×3 table
timeStart timeEnd timeDuration
_________ ________ ____________
06:44:46 06:45:44 00:00:58
06:56:53 06:57:48 00:00:55
07:11:13 07:12:21 00:01:08
07:27:06 07:28:15 00:01:09
07:38:22 07:39:19 00:00:57
07:52:06 07:53:02 00:00:56
08:01:43 08:02:30 00:00:47
08:12:52 08:13:39 00:00:47
08:28:23 08:29:19 00:00:56
08:40:42 08:41:35 00:00:53
08:54:25 08:55:23 00:00:58

  1 Comment

Khandakar Rashid
Khandakar Rashid on 20 Jun 2020
Thank you so much Akira. Although I figured out a way to do this, your solution is much more elegant. I am going to use it :D :D

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by