function errorbarxy(x, y, stdy,stdx)
%ERRORBAR2 Add error bars to a plot.
%	errorbarxy(X,Y,Ey,Ex) adds error bars to an already existing plot of
%	vector X versus vector Y.  E is a vector the same length as X
%	and Y that specifies the lengths of the error bars.  The error
%	bars are each drawn a distance of E(i) above and below the
%	points in (X,Y) so that each bar is 2*E(i) long.

% Modified by JMB 7/89

hold on
npt = max(size(x));
if nargin == 2
	std = y;
	y = x;
	x(:) = 1:npt;
end

tee = (max(x)-min(x))/100;	% make tee .01 x-distance for error bars
xl = x - tee;
xr = x + tee;

if nargin == 4,
 teex = (max(y)-min(y))/100; % make tee .01 y-distance for error bars
 yb=y-teex; yt=y+teex;
 end

for i = 1:npt
	if abs(stdy(i))
		ytop = y(i) + stdy(i);
		ybot = y(i) - stdy(i);
		plot([x(i) x(i)], [ytop ybot],'-',[xl(i) xr(i)], [ytop ytop],'-',[xl(i) xr(i)],[ybot ybot],'-')
  if nargin==4,
   xright=x(i)+stdx(i);
   xleft=x(i)-stdx(i);
   plot([xleft xright ], [y(i) y(i)],'-',[xleft xleft], [yt(i) yb(i)],'-',[xright xright],[yt(i) yb(i)],'-')
   end
  
	end
end
hold off
