function RespObj = newunits(RespObj,type,OldUnit);
%NEWUNITS  Apply changes to a response object due to a unit change
%
%   NEWUNITS(RespObj,type,OldUnit) applies the necessary changes to the
%   response object RespObj when there has been a unit change.  The type
%   of unit change is given in "type" and the old unit value is given in
%   "OldUnit".  NEWUNITS should only be called from the SET command of
%   the associated RespObj.

%   Authors: Karen Gondoly, 3-27-98
%   Revised: Adam DiVergilio, 11-99
%   Copyright 1986-2000 The MathWorks, Inc. 
%   $Revision: 1.13 $  $Date: 2000/08/29 22:08:24 $

allProps = get(RespObj.response,'Undocumented');
LTIdisplayAxes = allProps.PlotAxes;
XLimMode = allProps.XlimMode;
YLimMode = allProps.YlimMode;
RefreshAxesLimits = 0;

%---Turn all dotted Gain/Phase margin lines off, so the scale matches the bode plot
k = findall(allProps.PlotAxes);
MarginLines = [findobj(k,'Tag','PhaseMarginHorizontalLine');
               findobj(k,'Tag','PhaseMarginVerticalLine'); 
               findobj(k,'Tag','PhaseMarginLine');
               findobj(k,'Tag','GainMarginHorizontalLine');
               findobj(k,'Tag','GainMarginVerticalLine'); 
               findobj(k,'Tag','GainMarginLine')];
set(MarginLines,'visible','off');

switch type

case 'frequency'
   NewUnit = RespObj.FrequencyUnits;

   %---Convert data
   AllLines = [findobj(k,'Tag','LTIresponseLines');
               findall(k,'Tag','NyquistLines')];
   AllXdata = get(AllLines,{'Xdata'});
   for ctLines = 1:length(AllXdata)
      AllXdata{ctLines} = unitconv(AllXdata{ctLines},OldUnit,NewUnit);
   end
   set(AllLines,{'Xdata'},AllXdata);

   %---Convert plot options
   StabMarg = RespObj.StabilityMarginValue;
   if ~isempty(StabMarg) & ~isempty(StabMarg(1).GMFrequency)
      for ctSM=1:length(StabMarg)
         StabMarg(ctSM).GMFrequency = unitconv(StabMarg(ctSM).GMFrequency,OldUnit,NewUnit);
         StabMarg(ctSM).PMFrequency = unitconv(StabMarg(ctSM).PMFrequency,OldUnit,NewUnit);
      end
      RespObj.StabilityMarginValue = StabMarg;
   end

   %---Adjust axes label
   set(RespObj,'Xlabel',sprintf('Frequency (%s)',NewUnit));

   %---Adjust axes limit mode
   RefreshAxesLimits = 1;
   XLimMode = 'auto';

case 'magnitude'
   NewUnit = RespObj.MagnitudeUnits;

   %---Convert data (skip phase data)
   AllLines = [findobj(LTIdisplayAxes(1:2:end,:),'Tag','LTIresponseLines');
               findall(LTIdisplayAxes(1:2:end,:),'Tag','NyquistLines')];
   AllYdata = get(AllLines,{'Ydata'});
   for ctLines=1:length(AllYdata)
      AllYdata{ctLines} = unitconv(AllYdata{ctLines},OldUnit,NewUnit);
   end
   set(AllLines,{'Ydata'},AllYdata);

   %---Convert plot options
   StabMarg = RespObj.StabilityMarginValue;
   if ~isempty(StabMarg) & ~isempty(StabMarg(1).GainMargin)
      for ctSM=1:length(StabMarg)
         StabMarg(ctSM).GainMargin = unitconv(StabMarg(ctSM).GainMargin,OldUnit,NewUnit);
      end
      RespObj.StabilityMarginValue = StabMarg;
   end

   %---Adjust axes label
   Ystr = get(RespObj.response,'Ylabel');
   indColon = findstr(';',Ystr);
   Ylabel = sprintf('Magnitude (%s)',NewUnit);
   set(RespObj,'Ylabel',sprintf('%s%s',Ystr(1:indColon+1),Ylabel));

   %---Force linear scale after unit change 
   set(LTIdisplayAxes,'YScale','linear');

   %---Adjust axes limit mode
   RefreshAxesLimits = 1;
   YLimMode = 'auto';

case 'phase'
   NewUnit = RespObj.PhaseUnits;

   %---Convert data (skip mag data)
   AllLines = [findobj(LTIdisplayAxes(2:2:end,:),'Tag','LTIresponseLines');
               findall(LTIdisplayAxes(2:2:end,:),'Tag','NyquistLines')];
   AllYdata = get(AllLines,{'Ydata'});
   for ctLines=1:length(AllYdata)
      AllYdata{ctLines} = unitconv(AllYdata{ctLines},OldUnit,NewUnit);
   end
   set(AllLines,{'Ydata'},AllYdata);

   %---Convert plot options
   StabMarg = RespObj.StabilityMarginValue;
   if ~isempty(StabMarg) & ~isempty(StabMarg(1).PhaseMargin)
      for ctSM=1:length(StabMarg)
         StabMarg(ctSM).PhaseMargin = unitconv(StabMarg(ctSM).PhaseMargin,OldUnit,NewUnit);
      end
      RespObj.StabilityMarginValue = StabMarg;
   end

   %---Adjust axes label
   Ystr = get(RespObj.response,'Ylabel');
   indColon = findstr(';',Ystr);
   Ylabel = sprintf('Phase (%s)',NewUnit);
   set(RespObj,'Ylabel',sprintf('%s%s',Ylabel,Ystr(indColon:end)));

   %---Adjust axes limit mode
   RefreshAxesLimits = 1;
   YLimMode = 'auto';

end

%---Delete all linescan markers in axes
linescan('deleteall',LTIdisplayAxes);

%---Rescale axes
if RefreshAxesLimits
   set(RespObj,'XlimMode',XLimMode,'YlimMode',YLimMode);
end

%---Gain text
if strncmpi(RespObj.MagnitudeUnits,'d',1)
   mstr = sprintf(' %s',RespObj.MagnitudeUnits);
else
   mstr = sprintf('');
end
if isinf(StabMarg(end).GainMargin)
   tStr = sprintf('Gm = Inf');
elseif ~unitconv(StabMarg(end).GainMargin,RespObj.MagnitudeUnits,'db')
   tStr = sprintf('Gm = %0.5g%s',StabMarg(end).GainMargin,mstr);
else
   tStr = sprintf('Gm = %0.5g%s (at %0.5g %s)',...
      StabMarg(end).GainMargin,mstr,...
      StabMarg(end).GMFrequency,RespObj.FrequencyUnits);
end

%---Phase text
if isinf(StabMarg(end).PhaseMargin)
   tStr = sprintf('%s,  Pm = Inf',tStr);
elseif ~StabMarg(end).PhaseMargin
   tStr = sprintf('%s,  Pm = 0 (unstable closed loop)',tStr);
else
   tStr = sprintf('%s,  Pm = %0.5g %s (at %0.5g %s)',tStr,...
      StabMarg(end).PhaseMargin,RespObj.PhaseUnits,...
      StabMarg(end).PMFrequency,RespObj.FrequencyUnits);
end

set(get(allProps.PlotAxes(1,1),'Title'),'String',tStr);

%---Update margin lines
 Ylim1  = get(allProps.PlotAxes(1),'Ylim');
 Ylim2  = get(allProps.PlotAxes(2),'Ylim');
 RHmag  = allProps.ResponseHandles{1}{1}{:};
 RHph   = allProps.ResponseHandles{1}{2}{:};
 X      = get(RHph(1),'XData');
 Phdata = get(RHph(1),'YData');
 Xlim   = [min(X) max(X)];
 Wcg    = StabMarg.GMFrequency;
 Wcp    = StabMarg.PMFrequency;

 p180 = unitconv(180,'deg',RespObj.PhaseUnits);   
 m0 = unitconv(0,'db',RespObj.MagnitudeUnits);
 GmPhaseLine = p180 * round(interp1(X(:),Phdata(:),Wcg)/p180);
 PmPhaseLine = p180 * round((interp1(X(:),Phdata(:),Wcp)-StabMarg.PhaseMargin)/p180);
 if strncmpi(RespObj.MagnitudeUnits,'d',1)
    GmPlot = -1*StabMarg.GainMargin;
 else
    GmPlot = 1./StabMarg.GainMargin;
 end

 set(findobj(RHmag,'Tag','GainMarginLine'),...
    'Xdata',[Wcg;Wcg],'Ydata',[GmPlot;m0]);
 set(findobj(RHmag,'Tag','GainMarginHorizontalLine'),...
    'Xdata',Xlim,'Ydata',[m0;m0]);
 set(findobj(RHmag,'Tag','GainMarginVerticalLine'),...
    'Xdata',[Wcg;Wcg],'Ydata',[Ylim1(1);GmPlot]);
 set(findobj(RHph,'Tag','GainMarginVerticalLine'),...
    'Xdata',[Wcg;Wcg],'Ydata',[GmPhaseLine;Ylim2(2)]);  % ydata order is important (for axeslims)
 set(findobj(RHph,'Tag','GainMarginHorizontalLine'),...
    'Xdata',Xlim,'Ydata',[GmPhaseLine;GmPhaseLine]);

 set(findobj(RHph,'Tag','PhaseMarginLine'),...
    'Xdata',[Wcp;Wcp],'Ydata',[PmPhaseLine;StabMarg.PhaseMargin+PmPhaseLine]);
 set(findobj(RHph,'Tag','PhaseMarginHorizontalLine'),...
    'Xdata',Xlim,'Ydata',[PmPhaseLine;PmPhaseLine]);
 set(findobj(RHph,'Tag','PhaseMarginVerticalLine'),...
    'Xdata',[Wcp;Wcp],'Ydata',[StabMarg.PhaseMargin+PmPhaseLine;Ylim2(2)]);  % ydata order is important (for axeslims)
 set(findobj(RHmag,'Tag','PhaseMarginVerticalLine'),...
    'Xdata',[Wcp;Wcp],'Ydata',[Ylim1(1);m0]);
 set(findobj(RHmag,'Tag','PhaseMarginHorizontalLine'),...
    'Xdata',Xlim,'Ydata',[m0;m0]);

%---Turn all dotted Gain/Phase margin lines on
set(MarginLines,'visible','on');

%---Rescale axes again
if RefreshAxesLimits
   set(RespObj,'XlimMode',XLimMode,'YlimMode',YLimMode);
end
