function clegend4(z,label,width,sideflag);
%CLEGEND5 Plot color bar legend below a plot.
%
%    CLEGEND5(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.
%
%    CLEGEND5(Z,CLABEL) labels the axis of the color legend with the
%    string CLABEL.  CLEGEND4(Z,CLABEL,WIDTH) uses WIDTH to specify
%    the fractional width of the main plot area.  The default width is 0.83
%    CLEGEND5(Z,CLABEL,WIDTH,SIDEFLAG) specifies whether the flag will be
%    placed on the side or bottom default 0 is side.
%
%       If you want, say, CLEGEND to show N equally spaced colours then
%       create an N-color colormap.
%       
%       Must provide axes command in program calling this function or
%       plot will occur inside the color bar itself
%
%
%
%    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.  And by CVL for sideflag
if nargin==0, z = [0:16]/16; end
if nargin>=1, if isempty(z), z = [0:16]/16; end, end
if nargin<3, width = .83; end;
if nargin<4, sideflag=0; end;

  zmin = min(z(z~=NaN));
  zmax = max(z(z~=NaN));
  z = zmin + [0:255]*(zmax-zmin)/255;

clf;
if sideflag,
   HH=axes('position',[.195 .15 .7 (1-width-.12)]);
   pcolor(z,[0 10]',[z;z]);
   HH=gca;
   set(HH,'position',[.17 .15 .7 (1-width-.12)]);
   caxis([min(z) max(z)]);
   shading('interp');
   if nargin>=2, xlabel(label), end
% Note use of undocumented 'sc' and 'fontsize' flags....
   %text(.5,-2.5,[ 'P. Wiebe, WHOI: ' date],'sc','fontsize',10);
   set(gca,'tickdir','out','YTick',[]);
% Set up subplot for main graph
 %  axes('position', [.12 (1-width+.15) .80 width-.23]);
else
   HH=axes('position',[.95-(1-width-.12) .15 (1-width-.12) .7]);
   pcolor([0 10],z,[z;z]');
   HH=gca;
   set(HH,'position',[.95-(1-width-.12) .15 (1-width-.12) .7]);
   caxis([min(z) max(z)]);
   shading('interp');
   if nargin>=2, ylabel(label), end
% Note use of undocumented 'sc' and 'fontsize' flags....
  % text(1.7,-.2,[ 'P. Wiebe, WHOI: ' date],'sc','rotation',90,'fontsize',10);
   set(gca,'tickdir','out','XTick',[]);
% Set up subplot for main graph
%   axes('position', [.12 .12 .85-(1-width) .8]);
end;

