function f = errorbar2(x,y,er,direction,varargin) <x>,<y>,<er> are row vectors of the same length if <er> has imaginary numbers (determined via any(imag(<er>)~=0)), then real(<er>) and imag(<er>) determines the downward and upward (or leftward and rightward) extent of the error bars, respectively. <er> can also be 2 x N where the first row has values for the lower bound and the second row has values for the upper bound. <direction> is 0 or 'h' or 'x' means error on x 1 or 'v' or 'y' means error on y <varargin> are additional arguments to plot.m (e.g. 'r-') draws error lines on the current figure, returning a vector of handles. example: figure; errorbar2(randn(1,10),randn(1,10),randn(1,10)/4,1,'r-'); TODO: what about the cases of NaNs? e.g. see errorbar3.m.
0001 function f = errorbar2(x,y,er,direction,varargin) 0002 0003 % function f = errorbar2(x,y,er,direction,varargin) 0004 % 0005 % <x>,<y>,<er> are row vectors of the same length 0006 % if <er> has imaginary numbers (determined via any(imag(<er>)~=0)), 0007 % then real(<er>) and imag(<er>) determines the downward and 0008 % upward (or leftward and rightward) extent of the error bars, 0009 % respectively. <er> can also be 2 x N where the first row 0010 % has values for the lower bound and the second row has values 0011 % for the upper bound. 0012 % <direction> is 0013 % 0 or 'h' or 'x' means error on x 0014 % 1 or 'v' or 'y' means error on y 0015 % <varargin> are additional arguments to plot.m (e.g. 'r-') 0016 % 0017 % draws error lines on the current figure, returning 0018 % a vector of handles. 0019 % 0020 % example: 0021 % figure; errorbar2(randn(1,10),randn(1,10),randn(1,10)/4,1,'r-'); 0022 % 0023 % TODO: what about the cases of NaNs? e.g. see errorbar3.m. 0024 0025 % hold on 0026 prev = ishold; 0027 hold on; 0028 0029 % prep er 0030 if size(er,1) == 2 0031 switch direction 0032 case {0 'h' 'x'} 0033 er = x - er(1,:) + j*(er(2,:) - x); 0034 case {1 'v' 'y'} 0035 er = y - er(1,:) + j*(er(2,:) - y); 0036 end 0037 end 0038 0039 % calc 0040 isimag = any(imag(er)~=0); 0041 0042 % do it 0043 f = []; 0044 if isimag 0045 switch direction 0046 case {0 'h' 'x'} 0047 for p=1:numel(x) 0048 f = [f plot([x(p)-real(er(p)) x(p)+imag(er(p))],[y(p) y(p)],varargin{:})]; 0049 end 0050 case {1 'v' 'y'} 0051 for p=1:numel(x) 0052 f = [f plot([x(p) x(p)],[y(p)-real(er(p)) y(p)+imag(er(p))],varargin{:})]; 0053 end 0054 otherwise 0055 error('invalid <direction>'); 0056 end 0057 else 0058 switch direction 0059 case {0 'h' 'x'} 0060 for p=1:numel(x) 0061 f = [f plot([x(p)-er(p) x(p)+er(p)],[y(p) y(p)],varargin{:})]; 0062 end 0063 case {1 'v' 'y'} 0064 for p=1:numel(x) 0065 f = [f plot([x(p) x(p)],[y(p)-er(p) y(p)+er(p)],varargin{:})]; 0066 end 0067 otherwise 0068 error('invalid <direction>'); 0069 end 0070 end 0071 0072 % hold off 0073 if ~prev 0074 hold off; 0075 end