How to write a value on a specific portion which is constant on a graph?

Hi ,
I want to write something on parts of a graph which are constant.
For example,
a=[1 2 2 2 2 2 2 2 2 2 3 5 5 5 5 5 5 5 ]
b=[1:1:length(a)]
plot(b,a)
My graph is attached.
For the portion on the xaxis, from 2 to 10, i want to write "level=2" on top of the constant line and from 12 to 18, i want to write "level 5" on top of the constant line.
Anyway of doing it in MATLAB?

2 Comments

Are you looking to automate this or just manually put in these values?
Hi Nick,
I want to automate. Find below part of my code and a .png of my graph. I should have been more accurate in my question. It is actually a DATETIME plot. So inserting it as a text does not work.
Numberofalt=length(out_select(:,1))
colorVec = hsv(length(out_select));
for i=1:Numberofalt
plot(time_select_20000(out_select(i,2):out_select(i,3)),Alt_select_20000(out_select(i,2):out_select(i,3)),'Color',colorVec(i,:),'LineWidth',2)
hold on
end
On top of each line , that i generate in the plot, i want to write down a corresponding value which corresponds to any of the values of
Alt_select_20000(out_select(i,2):out_select(i,3))
which is actually a constant number. See the graph i generate.

Sign in to comment.

 Accepted Answer

Try this:
a = [1,2,2,2,2,2,2,2,2,2,3,5,5,5,5,5,5,5];
b = 1:numel(a);
%
d = [1,diff(a),1]; % differences between values
s = find(d(1:end-1)); % begin of contiguous blocks
e = find(d(2:end)); % end of contiguous blocks
n = e-s;
x = n>0; % select how many must be in a block
p = mean([s(x);e(x)],1); % x-positions
v = a(s(x)); % y-positions
t = arrayfun(@(n)sprintf('level %g',n),v,'UniformOutput',false);
%
plot(b,a)
text(p,v,t,'VerticalAlignment','top')
Which generates this figure:
Note that I placed the text under the line as the axes do not resize automatically to fit the text. Of course you can change this and many other options, such as the horizontal alignment, typeface, font size etc. See the text properties for more info on this.

9 Comments

@Stephan
Thanks for your response. Does placing text in a datetime plot also work like this? My main issue is for putting a specific value on a datetime plot.
See the comment on top which same as the link :
@Stephan
Assume your b values were a datetime values. Would it still work? What do i need to change?
I have no idea, I have never used the datetime class... but I did answer your original question, which does not mention datetime anywhere.
It might work if you convert the datetime values to datenum's (i.e. as the a values), then the whole process would work much the same. I would recommend using a tolerance instead of the simple find though, something like this:
tol = 0.01; % pick fraction of day
s = find(abs(d(1:end-1))>tol);
e = find(abs(d(2:end))>tol);
I tried this
a = [1,2,2,2,2,2,2,2,2,2,3,5,5,5,5,5,5,5];
b1 = t2(1:18)%datetime array
b=datenum(b1)
d = [1,diff(a),1]; % differences between values
tol = 0.01; % pick fraction of day
s = find(abs(d(1:end-1))>tol);
e = find(abs(d(2:end))>tol);
n = e-s;
x = n>0; % how many in a row
p = mean([s(x);e(x)],1); % x-positions
v = a(s(x)); % y-positions
t = arrayfun(@(n)sprintf('level %g',n),v,'UniformOutput',false);
%
plot(b1,a)
text(p,v,t,'VerticalAlignment','top')
Still same. Nothing is displayed. Did you mean something like this?
Ah, my mistake, I thought your dependent variable was the datetime. Ignore my last comment, it makes no sense for your situation. Use the original code that I gave.
But text does not currently accept datetime values, so we have to figure out what the corresponding x values are that the the axes actually uses. According to this article it seems to involve datenum, which means that to get the datetime values corresponding to the contiguous value blocks we can do this (untested):
p = datenum(b1(round(mean([s(x);e(x)],1))));
and everything else is just like it was in my original answer.
If this does not work then you will probably want to have a look at some of the axes properties, such as XLim and XTick, and also the data X-values (see line properties). You could then figure out how to convert the datetime values given in p to the same scale, and use these converted values as the x input of the text call. Or perhaps this is documented somewhere...
Yea, this is exactly the main thing! With normal integer values on the x axis, we can easily place a text anywhere. But for a datetime plot, i still trying to find out how to place a text on the plot.
a = [1,2,2,2,2,2,2,2,2,2,3,5,5,5,5,5,5,5];
b1 = t2(1:18)%datetime
b=datenum(b1)
d = [1,diff(a),1]; % differences between values
s = find(d(1:end-1)); % begin of contiguous blocks
e = find(d(2:end)); % end of contiguous blocks
n = e-s;
x = n>0; % select how many must be in a block
p = datenum(b1(round(mean([s(x);e(x)],1))));
v = a(s(x)); % y-positions
t = arrayfun(@(n)sprintf('level %g',n),v,'UniformOutput',false);
plot(b,a)
text(p,v,t,'VerticalAlignment','top')
Only i want to change the xaxis to datetime!
If you want datetime, then you need to plot it:
plot(b1,a)
Hi yea now it works!! So the main thing was to use the datenum ! Awesome thanks!

Sign in to comment.

More Answers (0)

Categories

Asked:

on 1 Jul 2015

Commented:

on 2 Jul 2015

Community Treasure Hunt

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

Start Hunting!