Plot length of time between two events

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:

 Akzeptierte Antwort

Chris
Chris am 30 Mär. 2023
Bearbeitet: Chris am 30 Mär. 2023

0 Stimmen

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 Kommentare

Maria
Maria am 30 Mär. 2023
Thanks Chris.
I don't have a code, just an idea. Which I believe is more or less what you are suggesting, but mine is more "manual". I'll try patch. Thank you again.
Sure @Maria.
If you prefer error bars, something like the following might work.
nsens = 32; % Or however many sensors you have
clrs = lines(nsens);
x = mean([t.starttime, t.stoptime], 2);
y = t.sid;
err = (t.stoptime-t.starttime)/2;
figure
hold on
for idx = 1:size(x,1)
errorbar(x(idx), y(idx), err(idx), 'horizontal','Color',clrs(y(idx),:))
end
If you have a table that doesn't look like the table I described, the necessary syntax may be slightly different. Feel free to describe how you generated the table, or attach some files with data if you're loading it from a text file or spreadsheet or something.
Thanks Chris. I loaded the data from a cvs file.
I have attached the table I'm using in matlab, that is as you described: SID (double), AOS (datetime) and LOS (datetime).
I tried both patch and errorbar (btw, I don't have any preferences between them). For both I get a similar error, which is that the inputs have to be numeric data.
Below you can see the code, which is exactly as you sent just changing the names to what I have.
1) Using patch:
I get:
"Error using patch
Non-numeric data is not supported in 'patch'"
nsens = 32; % Or however many sensors you have
clrs = lines(nsens);
barhalfheight = 0.1;
x = [T2.AOS,T2.LOS,T2.AOS,T2.LOS];
y = T2.SID + barhalfheight.*[-1, -1, 1, 1];
c = clrs(T2.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))
2) Using errorbar:
I get:
"Error using errorbar (line 76)
Input arguments must be numeric or objects which can be converted to double."
nsens = 32; % Or however many sensors you have
clrs = lines(nsens);
x = mean([T2.AOS,T2.LOS], 2);
y = T2.SID;
err = (T2.AOS - T2.LOS)/2;
figure
hold on
for idx = 1:size(x,1)
errorbar(x(idx), y(idx), err(idx), 'horizontal','Color',clrs(y(idx),:))
end
Chris
Chris am 4 Apr. 2023
Hi @Maria,
Both these code snippets work for me in R2022b with what you've attached.
What version of Matlab are you using? Datetimes were introduced in 2014, but maybe errorbar and patch didn't support them until recently...
Maria
Maria am 4 Apr. 2023
Interesting, I have R2018a...
Chris
Chris am 5 Apr. 2023
Bearbeitet: Chris am 5 Apr. 2023
Then I'd suggest updating, if you can, or try Matlab Online. Otherwise, you may need to convert the datetimes to doubles with posixtime(), then figure out how to format the XTicks and XLabels to your liking. Note, this may not be the best method available, but it's the best I can find at the moment.
Maria
Maria am 5 Apr. 2023
Thanks @Chris. Don't worry, you have been great help. I'll post here if I'm able to do it, in case is useful for anyone else...
Maria
Maria am 5 Apr. 2023
using Matlab Online, worked for both ways, as expected.
converting datetime to posixtime worked too.
Thank you again @Chris!
Chris
Chris am 6 Apr. 2023
No problem. :)

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Gefragt:

am 30 Mär. 2023

Kommentiert:

am 6 Apr. 2023

Community Treasure Hunt

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

Start Hunting!

Translated by