%% pwerr2.m is the 2-D version of pwerr.m which plots
%% error bars in both axis.

function pwerr2(x,xe,y,ye,cv)

% x = x-values
% xe = error in y-values
% y = y-values
% ye = error in y-values
% cv = valid string to give plot colour

%convert all input to column vectors
x=x(:);
y=y(:);
xe=xe(:);
ye=ye(:);

%remove terms with 0 error in either axis
yec=ye==0 | xe==0;
if any(yec)
 x(yec)=[];
 y(yec)=[];
 xe(yec)=[];
 ye(yec)=[];
end 

xu=x+xe;                                        %upper x error values
xl=x-xe;                                        %lower x error values
yu=y+ye;                                        %upper y error values
yl=y-ye;                                        %lower y error values

xmx=max(x)-min(x);                              %seperation in x values
xub=x+0.01*xmx;                                 %upper x values for errorbar tops
xlb=x-0.01*xmx;                                 %lower x values for errorbar tops

ymx=max(y)-min(y);                              %seperation in y values
yub=y+0.01*ymx;                                 %upper y values for errorbar tops
ylb=y-0.01*ymx;                                 %lower y values for errorbar tops

h1=plot([x x]',[yu yl]',cv,[xlb xub]',[yl yl]',cv,[xlb xub]',[yu yu]',cv,[xu xl]',[y y]',cv,[xl xl]',[ylb yub]',cv,[xu xu]',[ylb yub]',cv);
set(h1,'linewidth',2)
