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

조회 수: 1 (최근 30일)
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
Nick
Nick 2015년 7월 1일
Are you looking to automate this or just manually put in these values?
yashvin
yashvin 2015년 7월 1일
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.

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

채택된 답변

Stephen23
Stephen23 2015년 7월 1일
편집: Stephen23 2015년 7월 1일
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
Stephen23
Stephen23 2015년 7월 2일
If you want datetime, then you need to plot it:
plot(b1,a)
yashvin
yashvin 2015년 7월 2일
Hi yea now it works!! So the main thing was to use the datenum ! Awesome thanks!

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

추가 답변 (0개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by