# quiverc2wcmap

## PURPOSE

function hh = quiverc2wcmap(varargin)

## SYNOPSIS

function hh = quiverc2wcmap(x,y,u,v,scale,vr,exag)

## DESCRIPTION

     function hh = quiverc2wcmap(varargin)
     Andrew Diamond 3/17/2005
     This is based off of Tobias Höffken which was based off of Bertrand Dano
     keeping with their main intent to show a color w/r to magnitude quiver plot
     while maintaining a large amount of quiver API backwards compatability.
     Functional differences from quiverc2:
       1) This works under 6.5.1
       2) It handles NaNs
       3) It draws a colormap that is w/r to the quiver magnitudes (hard coded to
       20 segments of the colormap as per quiverc2 - seems fine for a quiver).
       4) Various bug fixes (I think )
     In order to do this I needed some small hacks on 6.5.1's quiver to take a
     color triplet.  I have included as part of this file in a subfunciton below.
    ----------------
     Rest of comments from quiverc2
     changed Tobias Höffken 3-14-05
     totally downstripped version of the former
     split input field into n segments and do a quiver qhich each of them

## CROSS-REFERENCE INFORMATION

This function calls:

This function is called by:

## SUBFUNCTIONS

- function hh = quiver(varargin)
- function retval = isscalar(m)

## SOURCE CODE

    0001  function hh = quiverc2wcmap(x,y,u,v,scale,vr,exag)
    0002  % function hh = quiverc2wcmap(varargin)
    0003 % Andrew Diamond 3/17/2005
    0004 % This is based off of Tobias Höffken which was based off of Bertrand Dano
    0005 % keeping with their main intent to show a color w/r to magnitude quiver plot
    0006 % while maintaining a large amount of quiver API backwards compatability.
    0007 % Functional differences from quiverc2:
    0008 %   1) This works under 6.5.1
    0009 %   2) It handles NaNs
    0010 %   3) It draws a colormap that is w/r to the quiver magnitudes (hard coded to
    0011 %   20 segments of the colormap as per quiverc2 - seems fine for a quiver).
    0012 %   4) Various bug fixes (I think )
    0013 % In order to do this I needed some small hacks on 6.5.1's quiver to take a
    0014 % color triplet.  I have included as part of this file in a subfunciton below.
    0015 %----------------
    0016 % Rest of comments from quiverc2
    0017 % changed Tobias Höffken 3-14-05
    0018 % totally downstripped version of the former
    0019 % split input field into n segments and do a quiver qhich each of them
    0020
    0021 % Modified version of Quiver to plots velocity vectors as arrows
    0022 % with components (u,v) at the points (x,y) using the current colormap
    0023
    0024 % Bertrand Dano 3-3-03
    0025 % Copyright 1984-2002 The MathWorks, Inc.
    0026
    0027 % changed T. Höffken 14.03.05, for high data resolution
    0028 % using fixed color "spacing" of 20
    0029
    0030 %QUIVERC Quiver color plot.
    0031 %   QUIVERC(X,Y,U,V) plots velocity vectors as arrows with components (u,v)
    0032 %   at the points (x,y).  The matrices X,Y,U,V must all be the same size
    0033 %   and contain corresponding position and velocity components (X and Y
    0034 %   can also be vectors to specify a uniform grid).  QUIVER automatically
    0035 %   scales the arrows to fit within the grid.
    0036 %
    0037 %   QUIVERC(U,V) plots velocity vectors at equally spaced points in
    0038 %   the x-y plane.
    0039 %
    0040 %   QUIVERC(U,V,S) or QUIVER(X,Y,U,V,S) automatically scales the
    0041 %   arrows to fit within the grid and then stretches them by S.  Use
    0042 %   S=0 to plot the arrows without the automatic scaling.
    0043 %
    0044 %   QUIVERC(...,LINESPEC) uses the plot linestyle specified for
    0045 %   the velocity vectors.  Any marker in LINESPEC is drawn at the base
    0046 %   instead of an arrow on the tip.  Use a marker of '.' to specify
    0047 %   no marker at all.  See PLOT for other possibilities.
    0048 %
    0049 %   QUIVERC(...,'filled') fills any markers specified.
    0050 %
    0051 %   H = QUIVERC(...) returns a vector of line handles.
    0052 %
    0053 %   Example:
    0054 %      [x,y] = meshgrid(-2:.2:2,-1:.15:1);
    0055 %      z = x .* exp(-x.^2 - y.^2); [px,py] = gradient(z,.2,.15);
    0056 %      contour(x,y,z), hold on
    0057 %      quiverc(x,y,px,py), hold off, axis image
    0058 %
    0059 %   See also FEATHER, QUIVER3, PLOT.
    0060 %   Clay M. Thompson 3-3-94
    0061 %   Copyright 1984-2002 The MathWorks, Inc.
    0062 %   $Revision: 5.21 $  $Date: 2002/06/05 20:05:16 $
    0063 %-------------------------------------------------------------
    0064 n=64; % # of colors
    0065
    0066 % nin = nargin;
    0067 %
    0068 % error(nargchk(2,5,nin));
    0069 %
    0070 % % Check numeric input arguments
    0071 % if nin<4, % quiver(u,v) or quiver(u,v,s)
    0072 %   [msg,x,y,u,v] = xyzchk(varargin{1:2});
    0073 % else
    0074 %   [msg,x,y,u,v] = xyzchk(varargin{1:4});
    0075 % end
    0076 % if ~isempty(msg), error(msg); end
    0077 %
    0078 %
    0079 % scale=1; % %This is the default I think.
    0080 % if(nin == 3 | nin == 5)
    0081 %     if(isscalar(varargin{nin}))
    0082 %         scale = varargin{nin};
    0083 %     end
    0084 % end
    0085
    0086
    0087 %----------------------------------------------
    0088 % Define colormap
    0089 %vr = sqrt(u.^2+v.^2); % matrix of magnitudes of data
    0090 CC = colormap;
    0091 %--- commented out from quiverc2 -------------------------------------------
    0092 % scaled and ceiled matrix of magnitues so as to linearly scale them from 1:n so
    0093 % that they can be used and indicies into an n element array where n is defined
    0094 % (above) as being the number of colors;
    0095 % colit = ceil(((vr-min(vr(:)))./(max(vr(:))-min(vr(:))))*n);
    0096
    0097
    0098 %--- commented out from quiverc2 -------------------------------------------
    0099 % each ucell and vcell will cotain the portio of the data that corresponds to
    0100 % one of the n color bands (the 20 below should be a n)
    0101 % ucell = cell(n,1);
    0102 % vcell = cell(n,1);
    0103 % for it=(1:1:n) % initialize values to NaN
    0104 %     ucell{it}=ones(size(u))*NaN;
    0105 %     vcell{it}=ones(size(u))*NaN;
    0106 % end
    0107
    0108 % if data has Nan, don't let it contaminate the computations that segement the
    0109 % data;  I could just do this with vr and then get clever with the indices but
    0110 % this make for easy debugging and as this is a graphics routine the compuation
    0111 % time is completely irrelavent.
    0112 nonNaNind = find(~isnan(vr(:)));
    0113 xyuvvrNN(:,1) = x(nonNaNind);
    0114 xyuvvrNN(:,2) = y(nonNaNind);
    0115 xyuvvrNN(:,3) = u(nonNaNind);
    0116 xyuvvrNN(:,4) = v(nonNaNind);
    0117 xyuvvrNN(:,5) = vr(nonNaNind);
    0118 [xyuvvrNNs, xyuvvrNNsi] = sortrows(xyuvvrNN,5);
    0119
    0120 iCCs=round(linspace(1,size(CC,1),n));
    0121 iData = round(linspace(0,size(xyuvvrNNs,1),n+1));
    0122 % figure;
    0123
    0124 % -------------------
    0125 % Setup for a colormap?  Well, first, there must be a better way but anyway
    0126 % (at least in 6.5.1) if you ever want a colorbar tick marks to reflect real data ranges
    0127 % (versus just the indices of a colormap) then there apparently has to be an
    0128 % image somewhere in the figure.  Of course, I don't want an image but I figured
    0129 % I just make it invisible and then draw the quiver plot over it.
    0130 % Unfortunately, it seems that colorbar uses caxis to retrive the data range in
    0131 % the image and for invisible images it always seems to be 0 UNLESS you
    0132 % explictly reset the caxis.  Go figure (no pun intended).  Yeah, I would trust this, uhhuh (-;
    0133 % This will work but then the axis will be oversized to accomodate the image
    0134 % because images have their first and last vitual "pixels" CENTERED around the
    0135 % implicit or explict xmin,xmax,ymin,ymax (as per imagesc documentation) but the
    0136 % start and end of each of these "pixels" is +/- half a unit where the unit
    0137 % corresponds to subdividing the limits by the number of pixels (-1).  Given
    0138 % that formula and given my invisible 2x2 image for which it is desired to
    0139 % diplay in an axis that ISN'T oversized (i.e. min(x), max(x), min(y),max(y)) it
    0140 % is possible to solve for the limits (i.e. an artifically reduced limit)
    0141 % that need to be specified for imagesc to make it's final oversized axis to be the
    0142 % non oversized axis that we really want.
    0143 % xa,xb,ya,yb compenstates for the axis extention given by imagesc to make
    0144 % pixels centered at the limit extents (etc.).  Note, this "easy"
    0145 % formula would only work for 2x2 pixel images.
    0146 % xs = x(1);
    0147 % xf = x(end);
    0148 % xa = (3 * xs + xf)/4;
    0149 % xb = (3 * xf + xs)/4;
    0150 %
    0151 % ys = y(1);
    0152 % yf = y(end);
    0153 % ya = (3 * ys + yf)/4;
    0154 % yb = (3 * yf + ys)/4;
    0155 % h=imagesc([xa,xb],[ya,yb],reshape(xyuvvrNNs([1,end;1,end],5),2,2));
    0156 % set(h,'visible','off');
    0157 caxis(xyuvvrNNs([1,end],5));
    0158 %caxis([0 250])
    0159 hold on;
    0160 for it=1:n
    0161     c = CC(iCCs(it),:);
    0162     si = iData(it)+1;
    0163     ei = iData(it+1);
    0164     %hh=quiver(xyuvvrNNs(si:ei,1),xyuvvrNNs(si:ei,2),xyuvvrNNs(si:ei,3),xyuvvrNNs(si:ei,4),scale*it/n,'Color',c,exag);
    0165     hh=quiver(xyuvvrNNs(si:ei,1),xyuvvrNNs(si:ei,2),xyuvvrNNs(si:ei,3),xyuvvrNNs(si:ei,4),scale*it/n,'Color',[0 0 0],exag);  %Specifies single color (black) for all vectors
    0166 end
    0167
    0168 % colorbar;
    0169 %--- commented out from quiverc2 -------------------------------------------
    0170 % for jt=(1:1:length(u))
    0171 %     %it = ceil(((vr(jt)-min(vr(:)))/(max(vr(:))-min(vr(:))))*n);
    0172 %     ucell{min(n,max(colit(jt),1))}(jt) = u(jt);
    0173 %     vcell{min(n,max(colit(jt),1))}(jt) = v(jt);
    0174 % end
    0175 %
    0176 % figure;
    0177 % hold on;
    0178 %
    0179 %
    0180 % for it=(1:1:n)
    0181 %     c = CC(ceil(it/n*64),:);
    0182 %     hh=quiver(x,y,ucell{it},vcell{it},scale*it/n,'Color',c);
    0183 %     hold on;
    0184 % end
    0185 %
    0186
    0187
    0188
    0189
    0190 %-------------------------------
    0191 % This is Matlab's 6.5.1 quiver.  I figure that ensures a fair amouint of backward
    0192 % compatibility but I needed to hack it to allow a 'Color' property.  Obviously
    0193 % a person could do more.
    0194
    0195 function hh = quiver(varargin)
    0196 %QUIVER Quiver plot.
    0197 %   QUIVER(X,Y,U,V) plots velocity vectors as arrows with components (u,v)
    0198 %   at the points (x,y).  The matrices X,Y,U,V must all be the same size
    0199 %   and contain corresponding position and velocity components (X and Y
    0200 %   can also be vectors to specify a uniform grid).  QUIVER automatically
    0201 %   scales the arrows to fit within the grid.
    0202 %
    0203 %   QUIVER(U,V) plots velocity vectors at equally spaced points in
    0204 %   the x-y plane.
    0205 %
    0206 %   QUIVER(U,V,S) or QUIVER(X,Y,U,V,S) automatically scales the
    0207 %   arrows to fit within the grid and then stretches them by S.  Use
    0208 %   S=0 to plot the arrows without the automatic scaling.
    0209 %
    0210 %   QUIVER(...,LINESPEC) uses the plot linestyle specified for
    0211 %   the velocity vectors.  Any marker in LINESPEC is drawn at the base
    0212 %   instead of an arrow on the tip.  Use a marker of '.' to specify
    0213 %   no marker at all.  See PLOT for other possibilities.
    0214 %
    0215 %   QUIVER(...,'filled') fills any markers specified.
    0216 %
    0217 %   H = QUIVER(...) returns a vector of line handles.
    0218 %
    0219 %   Example:
    0220 %      [x,y] = meshgrid(-2:.2:2,-1:.15:1);
    0221 %      z = x .* exp(-x.^2 - y.^2); [px,py] = gradient(z,.2,.15);
    0222 %      contour(x,y,z), hold on
    0223 %      quiver(x,y,px,py), hold off, axis image
    0224 %
    0225 %   See also FEATHER, QUIVER3, PLOT.
    0226
    0227 %   Clay M. Thompson 3-3-94
    0228 %   Copyright 1984-2002 The MathWorks, Inc.
    0229 %   $Revision: 5.21 $  $Date: 2002/06/05 20:05:16 $
    0230
    0231 nin = nargin-1;
    0232
    0233 % Arrow head parameters
    0234 exag = varargin{end};
    0235 alpha = 0.33; % Size of arrow head relative to the length of the vector 0.33
    0236 beta = 0.33./exag;  % Width of the base of the arrow head relative to the length 0.33./exag
    0237 autoscale = 1; % Autoscale if ~= 0 then scale by this.
    0238 plotarrows = 1; % Plot arrows
    0239 sym = '';
    0240
    0241
    0242 filled = 0;
    0243 ls = '-';
    0244 ms = '';
    0245 col = 'b';
    0246
    0247 ColorSpecInd = find(strcmpi(varargin, 'color'));
    0248 if(length(ColorSpecInd)==1 & nin > ColorSpecInd)
    0249     col = varargin{ColorSpecInd+1};
    0250     varargin = varargin([1:ColorSpecInd-1,ColorSpecInd+2:nin]);
    0251     nin = nin-2;
    0252 end
    0253 % Parse the string inputs
    0254 while isstr(varargin{nin}),
    0255   vv = varargin{nin};
    0256   if ~isempty(vv) & strcmp(lower(vv(1)),'f')
    0257     filled = 1;
    0258     nin = nin-1;
    0259   else
    0260     [l,c,m,msg] = colstyle(vv);
    0261     if ~isempty(msg),
    0262       error(sprintf('Unknown option "%s".',vv));
    0263     end
    0264     if ~isempty(l), ls = l; end
    0265     if ~isempty(c), col = c; end
    0266     if ~isempty(m), ms = m; plotarrows = 0; end
    0267     if isequal(m,'.'), ms = ''; end % Don't plot '.'
    0268     nin = nin-1;
    0269   end
    0270 end
    0271
    0272 error(nargchk(2,8,nin));
    0273
    0274 % Check numeric input arguments
    0275 if nin<4, % quiver(u,v) or quiver(u,v,s)
    0276   [msg,x,y,u,v] = xyzchk(varargin{1:2});
    0277 else
    0278   [msg,x,y,u,v] = xyzchk(varargin{1:4});
    0279 end
    0280 if ~isempty(msg), error(msg); end
    0281
    0282 if nin==3 | nin==5, % quiver(u,v,s) or quiver(x,y,u,v,s)
    0283   autoscale = varargin{nin};
    0284 end
    0285
    0286 % Scalar expand u,v
    0287 if prod(size(u))==1, u = u(ones(size(x))); end
    0288 if prod(size(v))==1, v = v(ones(size(u))); end
    0289
    0290 if autoscale,
    0291   % Base autoscale value on average spacing in the x and y
    0292   % directions.  Estimate number of points in each direction as
    0293   % either the size of the input arrays or the effective square
    0294   % spacing if x and y are vectors.
    0295   if min(size(x))==1, n=sqrt(prod(size(x))); m=n; else [m,n]=size(x); end
    0296   delx = diff([min(x(:)) max(x(:))])/n;
    0297   dely = diff([min(y(:)) max(y(:))])/m;
    0298   del = delx.^2 + dely.^2;
    0299   if del>0
    0300     len = sqrt((u.^2 + v.^2)/del);
    0301     maxlen = max(len(:));
    0302   else
    0303     maxlen = 0;
    0304   end
    0305
    0306   if maxlen>0
    0307     autoscale = autoscale*0.9 / maxlen;
    0308   else
    0309     autoscale = autoscale*0.9;
    0310   end
    0311   u = u*autoscale; v = v*autoscale;
    0312 end
    0313
    0314 ax = newplot;
    0315 next = lower(get(ax,'NextPlot'));
    0316 hold_state = ishold;
    0317
    0318 % Make velocity vectors
    0319 x = x(:).'; y = y(:).';
    0320 u = u(:).'; v = v(:).';
    0321 uu = [x;x+u;repmat(NaN,size(u))];
    0322 vv = [y;y+v;repmat(NaN,size(u))];
    0323
    0324 % h1 = plot(uu(:),vv(:),[col ls]);
    0325 h1 = plot(uu(:),vv(:),ls,'Color',col);
    0326
    0327 if plotarrows,
    0328   % Make arrow heads and plot them
    0329   hu = [x+u-alpha*(u+beta*(v+eps));x+u; ...
    0330         x+u-alpha*(u-beta*(v+eps));repmat(NaN,size(u))];
    0331   hv = [y+v-alpha*(v-beta*(u+eps));y+v; ...
    0332         y+v-alpha*(v+beta*(u+eps));repmat(NaN,size(v))];
    0333   hold on
    0334  %  h2 = plot(hu(:),hv(:),[col ls]);
    0335   h2 = plot(hu(:),hv(:),ls,'Color',col);
    0336 else
    0337   h2 = [];
    0338 end
    0339
    0340 if ~isempty(ms), % Plot marker on base
    0341   hu = x; hv = y;
    0342   hold on
    0343 %  h3 = plot(hu(:),hv(:),[col ms]);
    0344   h3 = plot(hu(:),hv(:),ls,'Color',col);
    0345   if filled, set(h3,'markerfacecolor',get(h1,'color')); end
    0346 else
    0347   h3 = [];
    0348 end
    0349
    0350 if ~hold_state, hold off, view(2); set(ax,'NextPlot',next); end
    0351
    0352 if nargout>0, hh = [h1;h2;h3]; end
    0353
    0354
    0355
    0356 function retval = isscalar(m)
    0357 retval = prod(size(m)) == 1;