function [hvec,hlab] = quivlabel(u,autoscale,units,loc)
% Draws a scale vector for a quiver plot. 
% 
% U([1 2]) are the vector components
%
% AUTOSCALE is the value saved from the quiver function output
%
% UNITS (string) is incorporated in the label placed next to the vector
% The magnitude of the vector quoted in the label is calculated from U.
%
% LOCATION (optional) is where to place the foot of the scale vector.
% If location is not given, ginput is invoked to select the location 
% manually.
% 
% Outputs are the handles to the vector and label
%
% [hvec,hlab] = quivlabel(u,autoscale,unitstr,location)

u = u(:);

if nargin < 4
   loc = ginput(1);
end
if nargin < 3
   units = [];
end

hold_state = get(gca,'nextplot');
set(gca,'nextplot','add')

% draw the scale vector [u(1) u(2)] using the autoscale value saved
% from quiver
hv = quiver(loc(1),loc(2),autoscale*u(1),autoscale*u(2),0);

% put a label next to the vector 
label = [' ' num2str(sqrt(u'*u)) ' ' units ' '];
hl = text(loc(1),loc(2),label);
if u(1) > 0
  set(hl,'HorizontalAlignment','right');
end

if nargout > 0
   hvec = hv;
end
if nargout > 1
   hlab = hl;
end

set(gca,'nextplot',hold_state)
