function [hd,hb]=errorbar2(x, y, l,u,le,ri,symbol,symbol2)
% function [hd,hb]=errorbar2(x,y,l,u,le,ri,symbol,symbol2)
%
% Plot graph with error bars.
%
% input  :	x		: vector of x-locations of data
%		y		: vector of y-locations of data
%		l		: vector of 'lower' errorbars
%		u		: vector of 'upper' errorbars
%		le		: vector of 'left' errorbars 
%		ri 		: vector of 'right' errorbars 
%		symbol		: color and line style of data line
%		symbol2		: color and line style of errorbars
%
% output :	hd		: handles to data line
%		hb		: handles to bars
%
% for unknown arguments please use []
%
% uses :	nmin, nmax
%
% version 1.0.2		last change 22.09.1997

%	L. Shure 5-17-88, 10-1-91 B.A. Jones 4-5-93
%	Copyright (c) 1984-94 by The MathWorks, Inc.
%	added SYMBOL2	G.Krahmann, IfM Kiel, 1995/03/09
%	revised for x-y bars 	G.Krahmann, IfM Kiel, 1995/11/16
%	changed maxnan to nmax	G.Krahmann, LODYC Paris, 1997/09/22
%	added markersize/lw op	G.Krahmann, LDEO, 1998/10/06  

if nargin<2
  disp('not enough input arguments')
  return
end
if all(size(x)~=1) & all(size(x)~=1)
  disp('errorbar2 can only handle vectors !')
  return
end
if nargin<3
  l=nan*x;
  u=nan*x;
  ri=nan*x;
  le=nan*x;
  symbol='-';
  symbol2='-';
  arg1 = 0.5;
elseif nargin<4
  u=l;
  ri=nan*x;
  le=nan*x;
  symbol='-';
  symbol2='-';
  arg1 = 0.5;
elseif nargin<5
  ri=nan*x;
  le=nan*x;
  symbol='-';
  symbol2='-';
  arg1 = 0.5;
elseif nargin<6
  ri=le;
  symbol='-';
  symbol2='-';
  arg1 = 0.5;
elseif nargin<7
  symbol='-';
  symbol2='-';
  arg1 = 0.5;
elseif nargin<8
  symbol2=symbol;
  if strcmp(symbol,'.')
    arg1 = 5;
  else
    arg1 = 0.5;
  end 
end
if isempty(x) | all(isnan(x))
  x=[1:length(y)];
end
if isempty(l) & isempty(u)
  l=nan*x;
  u=nan*x;
elseif isempty(l)
  l=u;
elseif isempty(u)
  u=l;
end
if isempty(le) & isempty(ri)
  le=nan*x;
  ri=nan*x;
elseif isempty(le)
  le=ri;
elseif isempty(ri)
  ri=le;
end
if all(isnan(l)) & any(~isnan(u))
  l=u;
elseif all(isnan(u)) & any(~isnan(l))
  u=l;
end
if all(isnan(le)) & any(~isnan(ri))
  le=ri;
elseif all(isnan(ri)) & any(~isnan(le))
  ri=le;
end
x=x(:);
y=y(:);
l=l(:);
u=u(:);
le=le(:);
ri=ri(:);

if any(size(x)~=size(y)) | any(size(x)~=size(l)) |  any(size(x)~=size(u)) ...
   any(size(x)~=size(u)) | any(size(x)~=size(le)) |  any(size(x)~=size(ri))
  error('The sizes of X, Y, L, U, LE and RI must be the same.');
end

tee = (nmax(x(:))-nmin(x(:)))/100;  % make tee .02 x-distance for error bars
xl = x - tee;
xr = x + tee;
tee = (nmax(y(:))-nmin(y(:)))/100;  % make tee .02 x-distance for error bars
yl = y - tee;
yr = y + tee;
n = size(y,2);

% Plot graph and bars
cax = newplot;
next = lower(get(cax,'NextPlot'));
% build up nan-separated vector for bars
xb = [];
yb = [];
xb2 = [];
yb2 = [];
nnan = nan*ones(1,n);
for i = 1:length(x)
    ytop = y(i,:) + u(i,:);
    ybot = y(i,:) - l(i,:);
    xb = [xb; x(i,:); x(i,:) ; nnan; xl(i,:);xr(i,:);nnan ;xl(i,:);xr(i,:);nnan];
    yb = [yb; ytop;ybot;nnan;ytop;ytop;nnan;ybot;ybot;nnan];

    xtop = x(i,:) + ri(i,:);
    xbot = x(i,:) - le(i,:);
    yb2 = [yb2; y(i,:); y(i,:) ; nnan; yl(i,:);yr(i,:);nnan ;yl(i,:);yr(i,:);nnan];
    xb2 = [xb2; xtop;xbot;nnan;xtop;xtop;nnan;xbot;xbot;nnan];

end

%plot(xb,yb,symbol2,x,y,symbol,xb2,yb2,symbol2)
hb=plot(xb,yb,symbol2,xb2,yb2,symbol2,x,y,symbol);
hd = hb(3);
hb = hb(1:2);
