Plot length of time between two events

조회 수: 5 (최근 30일)
Maria
Maria 2023년 3월 30일
댓글: Chris 2023년 4월 6일
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:

채택된 답변

Chris
Chris 2023년 3월 30일
편집: Chris 2023년 3월 30일
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
Maria
Maria 2023년 4월 5일
using Matlab Online, worked for both ways, as expected.
converting datetime to posixtime worked too.
Thank you again @Chris!
Chris
Chris 2023년 4월 6일
No problem. :)

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 MATLAB에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by