function clegend_h = pclegen_vert(z,geometry,labelstr,to_zero)
%function clegend_h = pclegen2(z,geometry,to_zero)
% pclegend( range, geometry, fig_window, label)
%   need to modify to position relative to the current axes--
%   need to make allowances for the labels.  Possibly move it around 
%       with a mouse.  
%   Plot color bar legend anywhere on a plot.
%
%	PCLEGEND(Z) produces a color bar legend with axis Z.
%	The matrix Z is used to determine the minimum and maximum axis 
%	labels for the color bar. The bar will be horizontal/vertical
%       depending on whether # rows </> # columns of Z.  It is fairly easy
%       to use this if Z = [min_val max_val]'
%
%	PCLEGEND(Z,GEOM) produces the same legend, but at a specific 
%       location on the figure.  The GEOM vector is specified with 
%       [ x y width height ], in normalized coordinates.
%       
%	PCLEGEND(Z,GEOM,FIG,LABEL) does the same with a y axis label and a
%	particular figure window specified.  
 
%       All parameters are optional.  
%
%	Clay M. Thompson  5-28-91
%	Copyright (c) 1991 by the MathWorks, Inc.
%
%       Fudged by me (RP) 9/jan/92 for beta 2, and again on 20/Mar/92 for 
%       beta 3.
%
%       John Evans, 10-26-95
%       Changed to allow for positioning and specified figure windows.
%
%       Aaron Bever, 2007
%       Changed to create a vertical colorbar, not horizontal.
%
%

if nargin<1
  z = [0:16]/16; 
end

if nargin < 2
  geometry = [ 0.195 0.15 0.7 0.05 ]; 
end


if nargin>=1
  if isempty(z) 
    z = [0:16]/16; 
  end 
end

[n,m]=size(z);
if n>1;z=z';end;
n=length(z);
zmin=min(z);
zmax=max(z);
z=[z 2*z(n)-z(n-1)]';
axhandle=gca;

pos0=get(gca,'position');
geometry(1)=pos0(1)+geometry(1)*pos0(3);
geometry(2)=pos0(2)+geometry(2)*pos0(4);
geometry(3)=geometry(3)*pos0(3);
geometry(4)=geometry(4)*pos0(4);

clegend_h=axes('position', geometry);
   contourf([0 1],z,[z z],[z]);hold on;
   contour([0 1],z,[z z],[z],'-k');
   hold off;
   if nargin<4
      axis([0 1 min(z) max(z)]);
else;
      axis([0 1 0 max(z)]);
   end;
   
   clegend_h=gca;
   clim=get(axhandle,'clim');
   caxis(clim); 
   shading('flat');
   set(gca,'tickdir','out','XTick',[],'xticklabel',[]);
   set(gca,'Ytick',[zmin zmax]);
  
   newbord
   if nargin>2;
      xlabel(labelstr);
   end;
   
set(gcf,'currentaxes',axhandle);

return;
