1function hhh=vline(x,in1,in2)
2% function h=vline(x, linetype, label)
3%
4% Draws a vertical line on the current axes at the location specified by 'x'. Optional arguments are
5% 'linetype' (default is 'r:') and 'label', which applies a text label to the graph near the line. The
6% label appears in the same color as the line.
7%
8% The line is held on the current axes, and after plotting the line, the function returns the axes to
9% its prior hold state.
10%
11% The HandleVisibility property of the line object is set to "off", so not only does it not appear on
12% legends, but it is not findable by using findobj. Specifying an output argument causes the function to
13% return a handle to the line, so it can be manipulated or deleted. Also, the HandleVisibility can be
14% overridden by setting the root's ShowHiddenHandles property to on.
15%
16% h = vline(42,'g','The Answer')
17%
18% returns a handle to a green vertical line on the current axes at x=42, and creates a text object on
19% the current axes, close to the line, which reads "The Answer".
20%
21% vline also supports vector inputs to draw multiple lines at once. For example,
22%
23% vline([4 8 12],{'g','r','b'},{'l1','lab2','LABELC'})
24%
25% draws three lines with the appropriate labels and colors.
26%
27% By Brandon Kuczenski for Kensington Labs.
28% brandon_kuczenski@kensingtonlabs.com
29% 8 November 2001
31if length(x)>1 % vector input
32 for I=1:length(x)
33 switch nargin
34 case 1
35 linetype='r:';
36 label='';
37 case 2
38 if ~iscell(in1)
39 in1={in1};
40 end
41 if I>length(in1)
42 linetype=in1{end};
43 else
44 linetype=in1{I};
45 end
46 label='';
47 case 3
48 if ~iscell(in1)
49 in1={in1};
50 end
51 if ~iscell(in2)
52 in2={in2};
53 end
54 if I>length(in1)
55 linetype=in1{end};
56 else
57 linetype=in1{I};
58 end
59 if I>length(in2)
60 label=in2{end};
61 else
62 label=in2{I};
63 end
64 end
65 h(I)=vline(x(I),linetype,label);
66 end
67else
68 switch nargin
69 case 1
70 linetype='r:';
71 label='';
72 case 2
73 linetype=in1;
74 label='';
75 case 3
76 linetype=in1;
77 label=in2;
78 end
83 g=ishold(gca);
84 hold on
86 y=get(gca,'ylim');
87 h=plot([x x],y,linetype);
88 if length(label)
89 xx=get(gca,'xlim');
90 xrange=xx(2)-xx(1);
91 xunit=(x-xx(1))/xrange;
92 if xunit<0.8
93 text(x+0.01*xrange,y(1)+0.1*(y(2)-y(1)),label,'color',get(h,'color'))
94 else
95 text(x-.05*xrange,y(1)+0.1*(y(2)-y(1)),label,'color',get(h,'color'))
96 end
97 end
99 if g==0
100 hold off
101 end
102 set(h,'tag','vline','handlevisibility','off')
103end % else
105if nargout
106 hhh=h;
107end