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/07/27 21:53:51 $

allProps = get(RespObj.response,'Undocumented');
LTIdisplayAxes = allProps.PlotAxes;
AllLines = [findobj(LTIdisplayAxes,'Tag','LTIresponseLines');
            findall(LTIdisplayAxes,'Tag','NyquistLines')];
XLimMode = allProps.XlimMode;
YLimMode = allProps.YlimMode;
RefreshAxesLimits = 0;

switch type

case 'frequency'
   NewUnit = RespObj.FrequencyUnits;

   %---Convert data
   Freq = RespObj.Frequency;
   for ct=1:length(Freq)
      for ctArray = 1:length(Freq{ct})
         Freq{ct}{ctArray} = unitconv(Freq{ct}{ctArray},OldUnit,NewUnit);
      end
   end
   RespObj.Frequency = Freq;

   %---Convert plot options
   PeakResp = RespObj.PeakResponseValue;
   if ~isempty(PeakResp) & ~isempty(PeakResp(1).Frequency)
      for ctPR=1:length(PeakResp)
         PeakResp(ctPR).Frequency = unitconv(PeakResp(ctPR).Frequency,OldUnit,NewUnit);
      end
      RespObj.PeakResponseValue = PeakResp;
   end

   %---Stability margins (min)
   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

   %---Stability margins (all)
   AllStabMarg = RespObj.AllStabilityMarginValue;
   for ctSM=1:length(AllStabMarg)
       nmodels = prod(size(AllStabMarg(ctSM).Margins));
       for n=1:nmodels
          AllStabMarg(ctSM).Margins(n).GMFrequency = unitconv(AllStabMarg(ctSM).Margins(n).GMFrequency,OldUnit,NewUnit);
          AllStabMarg(ctSM).Margins(n).PMFrequency = unitconv(AllStabMarg(ctSM).Margins(n).PMFrequency,OldUnit,NewUnit);
          AllStabMarg(ctSM).Margins(n).DMFrequency = unitconv(AllStabMarg(ctSM).Margins(n).DMFrequency,OldUnit,NewUnit);
       end
   end
   RespObj.AllStabilityMarginValue = AllStabMarg;

case 'magnitude'
   NewUnit = RespObj.MagnitudeUnits;

   %---Convert data
   AllYdata = get(AllLines,{'Ydata'});
   for ctLines=1:length(AllYdata)
      AllYdata{ctLines} = unitconv(AllYdata{ctLines},OldUnit,NewUnit);
   end
   set(AllLines,{'Ydata'},AllYdata);

   %---Convert plot options
   PeakResp = RespObj.PeakResponseValue;
   if ~isempty(PeakResp) & ~isempty(PeakResp(1).Peak)
      for ctPR=1:length(PeakResp)
         PeakResp(ctPR).Peak = unitconv(PeakResp(ctPR).Peak,OldUnit,NewUnit);
      end
      RespObj.PeakResponseValue = PeakResp;
   end

   %---Stability margins (min)
   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

   %---Stability margins (all)
   AllStabMarg = RespObj.AllStabilityMarginValue;
   for ctSM=1:length(AllStabMarg)
      nmodels = prod(size(AllStabMarg(ctSM).Margins));
      for n=1:nmodels
         AllStabMarg(ctSM).Margins(n).GainMargin = unitconv(AllStabMarg(ctSM).Margins(n).GainMargin,OldUnit,NewUnit);
      end
   end
   RespObj.AllStabilityMarginValue = AllStabMarg;

   %---Adjust axes label
   set(RespObj,'Ylabel',sprintf('Open-Loop Gain (%s)',NewUnit));

   %---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
   AllXdata = get(AllLines,{'Xdata'});
   for ctLines=1:length(AllXdata)
      AllXdata{ctLines} = unitconv(AllXdata{ctLines},OldUnit,NewUnit);
   end
   set(AllLines,{'Xdata'},AllXdata);

   %---Convert plot options
   PeakResp = RespObj.PeakResponseValue;
   if ~isempty(PeakResp) & ~isempty(PeakResp(1).PhaseAtPeak)
      for ctPR=1:length(PeakResp)
         PeakResp(ctPR).PhaseAtPeak = unitconv(PeakResp(ctPR).PhaseAtPeak,OldUnit,NewUnit);
      end
      RespObj.PeakResponseValue = PeakResp;
   end

   %---Stability margins (min)
   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

   %---Stability margins (all)
   AllStabMarg = RespObj.AllStabilityMarginValue;
   for ctSM=1:length(AllStabMarg)
      nmodels = prod(size(AllStabMarg(ctSM).Margins));
      for n=1:nmodels
         AllStabMarg(ctSM).Margins(n).PhaseMargin = unitconv(AllStabMarg(ctSM).Margins(n).PhaseMargin,OldUnit,NewUnit);
      end
   end
   RespObj.AllStabilityMarginValue = AllStabMarg;

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

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

end

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

%---Redraw plot options
if strcmp(RespObj.PeakResponse,'on')
   set(RespObj,'PeakResponse','off')
   set(RespObj,'PeakResponse','on')
end
if strcmp(RespObj.StabilityMargin,'on')
   set(RespObj,'StabilityMargin','off')
   set(RespObj,'StabilityMargin','on')
end
if strcmp(RespObj.AllStabilityMargin,'on') 
   set(RespObj,'AllStabilityMargin','off')
   set(RespObj,'AllStabilityMargin','on')
end

%---Rescale axes (must be done after redraw of plot options)
if RefreshAxesLimits
   set(RespObj,'XlimMode',XLimMode,'YlimMode',YLimMode);
end

%---See if the units on the Array Selector need to be updated
RespCMenu = get(RespObj,'UIcontextMenu');
if isequal(get(RespCMenu.ArrayMenu,'Visible'),'on') & ishandle(get(RespCMenu.ArrayMenu,'UserData'))
   paramsel('#criterion',RespObj);          
end
