function f = straightline(value,direction,linestyle,rng) <value> is a vector of values <direction> is 'h' or 'y' means horizontal lines 'v' or 'x' means vertical lines <linestyle> is like 'r:' <rng> (optional) is [A B] with the range to restrict to. for example, when <direction> is 'h', then <rng> being [1 3] means to restrict the horizontal extent of the line to between 1 and 3. if [] or not supplied, use the full range of the current axis. draw lines on the current figure. return a vector of handles. example: figure; h = straightline(randn(1,10),'v','r-'); set(h,'LineWidth',2);
0001 function f = straightline(value,direction,linestyle,rng) 0002 0003 % function f = straightline(value,direction,linestyle,rng) 0004 % 0005 % <value> is a vector of values 0006 % <direction> is 0007 % 'h' or 'y' means horizontal lines 0008 % 'v' or 'x' means vertical lines 0009 % <linestyle> is like 'r:' 0010 % <rng> (optional) is [A B] with the range to restrict to. 0011 % for example, when <direction> is 'h', then <rng> being 0012 % [1 3] means to restrict the horizontal extent of the line 0013 % to between 1 and 3. if [] or not supplied, use the 0014 % full range of the current axis. 0015 % 0016 % draw lines on the current figure. 0017 % return a vector of handles. 0018 % 0019 % example: 0020 % figure; 0021 % h = straightline(randn(1,10),'v','r-'); 0022 % set(h,'LineWidth',2); 0023 0024 % input 0025 if ~exist('rng','var') || isempty(rng) 0026 rng = []; 0027 end 0028 0029 % hold on 0030 prev = ishold; 0031 hold on; 0032 0033 % do it 0034 ax = axis; 0035 f = zeros(1,length(value)); 0036 switch direction 0037 case {'h' 'y'} 0038 if isempty(rng) 0039 rng = ax(1:2); 0040 end 0041 for p=1:length(value) 0042 f(p) = plot(rng,[value(p) value(p)],linestyle); 0043 end 0044 case {'v' 'x'} 0045 if isempty(rng) 0046 rng = ax(3:4); 0047 end 0048 for p=1:length(value) 0049 f(p) = plot([value(p) value(p)],rng,linestyle); 0050 end 0051 otherwise 0052 error('invalid <direction>'); 0053 end 0054 0055 % hold off 0056 if ~prev 0057 hold off; 0058 end