how do I plot input / output data so as to have a continuous bar in that range and not two simple points for each output / input?
2 views (last 30 days)
Show older comments
Hello everyone! I have the following datetime vector, which contains the time (date and time) in which the patient has been away from home. The odd rows correspond to the moment he left the house, the even rows correspond to the moment he returned home. How do I make a horizontal bar graph in which there is time in the x axis and my vector in the y axis, so as to obtain continuous bars for the "out-entered" time intervals (in red in the figure)? Thanks in advance.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/193254/image.png)
3 Comments
jonas
on 6 Aug 2018
Does it have to be a bar graph? You can make a stacked horizontal bar with barh, but it's extremely slow. You could get similar results using plot, which would be much more effecient.
Accepted Answer
jonas
on 6 Aug 2018
Edited: jonas
on 6 Aug 2018
From what you've described so far, a bar graph does not seem optimal. What about this approach instead?
in=load('closing_opening.mat')
t=in.closing_opening;
t=reshape(t,2,length(t)/2)'
h=plot(t',ones(size(t))');
set(h,'linewidth',4);
It basically outputs what you showed in your sketch. Change the y-tick to your label of choice. If you want to plot multiple arrays, just make a for loop with a variable y-coordinate.
4 Comments
jonas
on 10 Aug 2018
Edited: jonas
on 10 Aug 2018
Okay, this is a bit more complicated than the original question but I'll try. Note that I've used the other method that I linked my previous comment. It's faster and looks better.
I've written something to start with. However, I really don't understand how pir_tab_night is structured, what it means, and how it relates to the other two .mat files you attached.
You can easily change color of individual bars, but the colors relate to the Z-value and the colormap.
%%Load data
out1=load('closing_opening.mat')
out2=load('tab_alarms_night.mat')
figure;hold on
%%Some properties
w=0.2 %%Half bar width
y=[1 3] %%y-values of the bars
%%First plot
x=out1.closing_opening';
x=[x;x];
y1=y(1);
y1=[(y1+w).*ones(1,length(x));(y1-w).*ones(1,length(x))];
z=rand(1,length(x));
z=repmat([1 0],1,length(x)/2)
z=[z;z];
h=surf(x,y1,z)
%%Repeat for second bar
x=out1.closing_opening'; %%I am reusing the same data
x=[x;x];
y1=y(2);
y1=[(y1+w).*ones(1,length(x));(y1-w).*ones(1,length(x))];
z=rand(1,length(x));
z=repmat([1 0],1,length(x)/2)
z=[z;z];
h=surf(x,y1,z)
%%plot some markers for alarms
x2=out2.tab_alarms_night;
y2=mean(y1(:))-0.3
[G,id]=findgroups(x2.Column1type)
hl=splitapply(@plot,x2.t,y2.*ones(length(x2.t),1),G)
%%Change colors
set(hl(1),'color','r','linestyle','none','marker','x')
set(hl(2),'color','b','linestyle','none','marker','x')
legend(hl,cellstr(id))
map = [1 1 1
1 0 1
0 1 1
1 0 0
0 1 0
0 0 1
0 0 0];
colormap(map)
view([0 90])
set(gca,'ylim',[0 5])
colorbar
%set(h,'edgecolor','none') %If you want to remove edges
More Answers (1)
Erica Corradi
on 10 Aug 2018
5 Comments
jonas
on 10 Aug 2018
Edited: jonas
on 10 Aug 2018
Okay, I kind of get it. In the closing_opening you have a green bar when the person is away (if I remember correctly). In the other one, you have a bunch of time-slots with different sensor names. As the code is written now, the time between those slots are marked in different colors. Sometimes there is a big gap in the pir-time-series, I assume this means the person is away from home?
Obvious solution is to concatenate both data sets into a single large timetable, with a special identifier for closing_opening. I've done this, and it looks quite OK. If my understanding is correct, this situation should never occur:
subject leaves
motion detected
subject returns
In fact, subject leaves should always be followed by subject returns, with no motion detected in between. I've checked the combined timetable and it looks OK so far (open_closing always occur in pairs).
But what about this sequence:
12-Mar-2018 06:59:55 dbuid-17
12-Mar-2018 06:59:58 dbuid-19
12-Mar-2018 09:57:47 open_close
12-Mar-2018 11:09:40 open_close
12-Mar-2018 13:45:31 open_close
12-Mar-2018 15:50:14 open_close
12-Mar-2018 21:02:31 dbuid-19
12-Mar-2018 21:02:36 dbuid-19
12-Mar-2018 21:02:40 dbuid-19
It seems motion is detected - subject leaves - subject comes back - subject leaves - subject comes back - motion detected 6 hours later. I don't know what this means, but be cautious with the interpretation.
Attached is a code you can start from. It will give you something similar to what you drew. Since the data set is now so large, there is no chance for me to check if it's correct.
I'll consider this closed now. If you have any other question, please post a new submission and make sure the problem is well stated. General advice: don't throw a bunch of data on someone who is trying to help with the technical aspects of MATLAB. Make it easy by condensing the data set down to a few lines.
See Also
Categories
Find more on Line Plots 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!