Plot length of time between two events

7 views (last 30 days)
Maria
Maria on 30 Mar 2023
Commented: Chris on 6 Apr 2023
I have a table with 3 columns: Sensor ID, Start Time, Stop Time, like this:
2 '20 Mar 2023 18:01:24.318' '20 Mar 2023 18:14:29.431'
2 '20 Mar 2023 20:01:17.651' '20 Mar 2023 20:13:36.931'
2 '21 Mar 2023 06:23:12.515' '21 Mar 2023 06:37:34.666'
2 '21 Mar 2023 08:25:49.680' '21 Mar 2023 08:34:08.341'
3 '20 Mar 2023 23:09:56.209' '20 Mar 2023 23:23:32.154'
3 '21 Mar 2023 01:17:08.676' '21 Mar 2023 01:31:28.003'
3 '21 Mar 2023 10:20:17.002' '21 Mar 2023 10:22:52.180'
3 '21 Mar 2023 12:21:13.756' '21 Mar 2023 12:37:30.450'
4 '20 Mar 2023 16:50:51.910' '20 Mar 2023 16:53:08.560'
4 '21 Mar 2023 01:11:16.404' '21 Mar 2023 01:19:06.159'
4 '21 Mar 2023 03:07:32.598' '21 Mar 2023 03:22:05.176'
4 '21 Mar 2023 13:31:47.726' '21 Mar 2023 13:43:56.867'
4 '21 Mar 2023 15:30:50.827' '21 Mar 2023 15:43:57.428'
And I want to plot, the sensor number on the y axis, and in the x axis the length or time between start time and stop time. Basically, I need like a horizontal error bar, where the "error" is the time between start and stop time.
I can create this "error" and plot, but I was wondering if there was an easier/mote straight forward way of doing.
Here is an example of the plot I want:

Accepted Answer

Chris
Chris on 30 Mar 2023
Edited: Chris on 30 Mar 2023
Without your code, it's difficult to say what is "easier." Errorbar plots are pretty simple.
You could use patch...
% My table is "t" with variables "sid (double)","starttime (datetime)", and "stoptime (datetime)"
nsens = 32; % Or however many sensors you have
clrs = lines(nsens);
barhalfheight = 0.1;
x = [t.starttime, t.stoptime, t.stoptime, t.starttime];
y = t.sid + barhalfheight.*[-1, -1, 1, 1];
c = clrs(t.sid,:)
figure
for idx = 1:size(x,1)
patch(x(idx,:),y(idx,:),c(idx,:))
end
set(gca,'YTick', 1:nsens)
set(gca,'YTickLabel', "Sensor " + (1:nsens))
  9 Comments
Maria
Maria on 5 Apr 2023
using Matlab Online, worked for both ways, as expected.
converting datetime to posixtime worked too.
Thank you again @Chris!

Sign in to comment.

More Answers (0)

Categories

Find more on Just for fun in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!