MATLAB Answers

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?

5 views (last 30 days)
Erica Corradi
Erica Corradi on 6 Aug 2018
Edited: jonas on 10 Aug 2018
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.


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.

Sign in to comment.

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?
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.


Show 1 older comment
Erica Corradi
Erica Corradi on 10 Aug 2018
If I want to add to my bar (closing_opening) other data coming from another table (called pir_tab_night), how do I do it? I also want to add another bar (below the first one) that contains all the various alarms (contained in a further table, called tab_alarms_night). Regarding the pir_tab_night, the names of the pir sensors (which will appear in the first graph with different colors) can be found in the column called Column1streamId; while, regarding the tab_alarms_night, the types of alarm (which will appear in the second graph with different colors) can be found in the column called Column1type. I would also like to have the following colors: for the first bar graph
green, closing_opening;
red, dbuid-17;
brown, dbuid-21;
cyan, dbuid-20;
magenta, dbuid-18;
blue, dbuid-19;
while for the second bar:
red, alarm/personleaving;
blue, alarm/flood;
black, alarm/smoke;
green, alarm/nobedreturn. Thanks in advance!!!
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
figure;hold on
%%Some properties
w=0.2 %%Half bar width
y=[1 3] %%y-values of the bars
%%First plot
z=repmat([1 0],1,length(x)/2)
%%Repeat for second bar
x=out1.closing_opening'; %%I am reusing the same data
z=repmat([1 0],1,length(x)/2)
%%plot some markers for alarms
%%Change colors
map = [1 1 1
1 0 1
0 1 1
1 0 0
0 1 0
0 0 1
0 0 0];
view([0 90])
set(gca,'ylim',[0 5])
%set(h,'edgecolor','none') %If you want to remove edges

Sign in to comment.

More Answers (1)

Erica Corradi
Erica Corradi on 10 Aug 2018
The plot is great fine, thank you very much! I can put only the first plot since the alarms are below of it. If I wanted to put together the pir_tab_night and the closing_opening in the first plot, how should I write?


Show 2 older comments
jonas on 10 Aug 2018
And what do you want each segment of the bar to represent? In the first one you have open/close. In the other one you just have a single time-series. Draw another picture?
Erica Corradi
Erica Corradi on 10 Aug 2018
I attached a drawing to make you understand how I would like the chart: the "dbuid-xx" are in the pir_tab_night table, in the column "Column1streamId". Each number relative to the "dbuid-xx" represents a specific room of the home of the analyzed subject. The vector that I called closing_opening represents the exits from home of the subject. So I would to obtain one bar that contains all these information: where is the subject when he stay at home; when and how long the subject stay away from home.
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.

Sign in to comment.


Community Treasure Hunt

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

Start Hunting!