function h=vline(x, linetype, label) Draws a vertical line on the current axes at the location specified by 'x'. Optional arguments are 'linetype' (default is 'r:') and 'label', which applies a text label to the graph near the line. The label appears in the same color as the line. The line is held on the current axes, and after plotting the line, the function returns the axes to its prior hold state. The HandleVisibility property of the line object is set to "off", so not only does it not appear on legends, but it is not findable by using findobj. Specifying an output argument causes the function to return a handle to the line, so it can be manipulated or deleted. Also, the HandleVisibility can be overridden by setting the root's ShowHiddenHandles property to on. h = vline(42,'g','The Answer') returns a handle to a green vertical line on the current axes at x=42, and creates a text object on the current axes, close to the line, which reads "The Answer". vline also supports vector inputs to draw multiple lines at once. For example, vline([4 8 12],{'g','r','b'},{'l1','lab2','LABELC'}) draws three lines with the appropriate labels and colors. By Brandon Kuczenski for Kensington Labs. brandon_kuczenski@kensingtonlabs.com 8 November 2001
0001 function hhh=vline(x,in1,in2) 0002 % function h=vline(x, linetype, label) 0003 % 0004 % Draws a vertical line on the current axes at the location specified by 'x'. Optional arguments are 0005 % 'linetype' (default is 'r:') and 'label', which applies a text label to the graph near the line. The 0006 % label appears in the same color as the line. 0007 % 0008 % The line is held on the current axes, and after plotting the line, the function returns the axes to 0009 % its prior hold state. 0010 % 0011 % The HandleVisibility property of the line object is set to "off", so not only does it not appear on 0012 % legends, but it is not findable by using findobj. Specifying an output argument causes the function to 0013 % return a handle to the line, so it can be manipulated or deleted. Also, the HandleVisibility can be 0014 % overridden by setting the root's ShowHiddenHandles property to on. 0015 % 0016 % h = vline(42,'g','The Answer') 0017 % 0018 % returns a handle to a green vertical line on the current axes at x=42, and creates a text object on 0019 % the current axes, close to the line, which reads "The Answer". 0020 % 0021 % vline also supports vector inputs to draw multiple lines at once. For example, 0022 % 0023 % vline([4 8 12],{'g','r','b'},{'l1','lab2','LABELC'}) 0024 % 0025 % draws three lines with the appropriate labels and colors. 0026 % 0027 % By Brandon Kuczenski for Kensington Labs. 0028 % brandon_kuczenski@kensingtonlabs.com 0029 % 8 November 2001 0030 0031 if length(x)>1 % vector input 0032 for I=1:length(x) 0033 switch nargin 0034 case 1 0035 linetype='r:'; 0036 label=''; 0037 case 2 0038 if ~iscell(in1) 0039 in1={in1}; 0040 end 0041 if I>length(in1) 0042 linetype=in1{end}; 0043 else 0044 linetype=in1{I}; 0045 end 0046 label=''; 0047 case 3 0048 if ~iscell(in1) 0049 in1={in1}; 0050 end 0051 if ~iscell(in2) 0052 in2={in2}; 0053 end 0054 if I>length(in1) 0055 linetype=in1{end}; 0056 else 0057 linetype=in1{I}; 0058 end 0059 if I>length(in2) 0060 label=in2{end}; 0061 else 0062 label=in2{I}; 0063 end 0064 end 0065 h(I)=vline(x(I),linetype,label); 0066 end 0067 else 0068 switch nargin 0069 case 1 0070 linetype='r:'; 0071 label=''; 0072 case 2 0073 linetype=in1; 0074 label=''; 0075 case 3 0076 linetype=in1; 0077 label=in2; 0078 end 0079 0080 0081 0082 0083 g=ishold(gca); 0084 hold on 0085 0086 y=get(gca,'ylim'); 0087 h=plot([x x],y,linetype); 0088 if length(label) 0089 xx=get(gca,'xlim'); 0090 xrange=xx(2)-xx(1); 0091 xunit=(x-xx(1))/xrange; 0092 if xunit<0.8 0093 text(x+0.01*xrange,y(1)+0.1*(y(2)-y(1)),label,'color',get(h,'color')) 0094 else 0095 text(x-.05*xrange,y(1)+0.1*(y(2)-y(1)),label,'color',get(h,'color')) 0096 end 0097 end 0098 0099 if g==0 0100 hold off 0101 end 0102 set(h,'tag','vline','handlevisibility','off') 0103 end % else 0104 0105 if nargout 0106 hhh=h; 0107 end