function dummy = updategain(Editor)
%UPDATEGAIN  Lightweight plot update when modifying the loop gain.

%   Author(s): P. Gahinet
%   Copyright 1986-2000 The MathWorks, Inc. 
%   $Revision: 1.7 $  $Date: 2000/06/14 20:21:52 $

% RE: Assumes gain does not change sign

% REVISIT 
dummy = [];

if strcmp(Editor.EditMode,'off') | strcmp(Editor.Visible,'off')
    % Editor is inactive
    return
end

% Get normalized open-loop and absolute value of compensator gain
NormOpenLoop = Editor.LoopData.OpenLoop;
CurrentGain = abs(getgain(Editor.LoopData));
CLPoles = fastrloc(NormOpenLoop,CurrentGain);
[Wn,Zeta] = damp(CLPoles,Editor.LoopData.Compensator.Ts);

% If new closed-loop poles lie beyond data extent, extend asymptotes
% to include new gain value. Otherwise, include current gain value 
% to make sure red squares lie on the locus
% RE: Do not update Editor.LocusGains/Roots to avoid
%     * introducing persistent large gains in first case
%     * uncontrolled growth in second case
Gains = Editor.LocusGains;
Roots = Editor.LocusRoots;
[NewGain,RefRoot] = extendlocus(Gains,Roots,CurrentGain);
if ~isempty(NewGain)
   % Extend locus
   NewRoot = matchlsq(RefRoot,fastrloc(NormOpenLoop,NewGain));
   Roots = [NewRoot,Roots];
   [Gains,is] = sort([NewGain,Gains]);
   Roots = Roots(:,is);
elseif length(Gains) & ~any(Gains==CurrentGain)
   % Insert current gain in locus data 
   idx = find(Gains>CurrentGain);
   Roots = [Roots(:,1:idx(1)-1) , ...
           matchlsq(Roots(:,idx(1)-1),CLPoles) , Roots(:,idx(1):end)];
end

% Update locus plot
HG = Editor.HG;
set(HG.Locus,{'Xdata'},num2cell(real(Roots),2),{'Ydata'},num2cell(imag(Roots),2))

% Update closed-loop pole location
Editor.ClosedPoles = CLPoles;    
set(HG.ClosedLoop,{'Xdata'},num2cell(real(CLPoles)))
set(HG.ClosedLoop,{'Ydata'},num2cell(imag(CLPoles)))

%---Update axis limits
optlims(Editor);  % update "optimal" limits
updatelims(Editor);

   
