function NicRespObj = pvset(NicRespObj,varargin)
%PVSET  Set properties of Response Objects.
%
%   RespObj = PVSET(RespObj,'Property1',Value1,'Property2',Value2,...)
%   sets the values of the properties with exact names 'Property1',
%   'Property2',...
%
%   See also SET.

%   Author(s): A. Potvin, 3-1-94
%   Revised: P. Gahinet, 4-1-96
%   Revised for Response Objects: K. Gondoly, 2-17-98
%   Revised: A. DiVergilio, 11-99
%   Copyright 1986-2000 The MathWorks, Inc. 
%   $Revision: 1.25 $  $Date: 2000/06/14 20:09:40 $

ni = nargin;
ContextMenu = get(NicRespObj.response,'UicontextMenu');
RespObjProp = logical(zeros(ni-1,1));

for i=1:2:ni-1
   % Set each Property Name/Value pair in turn. 
   Property = varargin{i};
   Value = varargin{i+1};
   if ischar(Value)
      Value = lower(Value);
   end

   switch Property
   case 'MagnitudeUnits'
      if strncmpi(Value,'a',1)
         NewUnit = 'abs';
      elseif strncmpi(Value,'d',1)
         NewUnit = 'dB';
      else
         error(sprintf('Bad value for response property %s.',Property))
      end
      OldUnit = NicRespObj.MagnitudeUnits;
      NicRespObj.MagnitudeUnits = NewUnit;
      if ~strcmpi(OldUnit,NewUnit)
         NicRespObj = newunits(NicRespObj,'magnitude',OldUnit);
      end

   case 'PhaseUnits'
      if strncmpi(Value,'d',1)
         NewUnit = 'deg';
      elseif strncmpi(Value,'r',1)
         NewUnit = 'rad';
      else
         error(sprintf('Bad value for response property %s.',Property))
      end
      OldUnit = NicRespObj.PhaseUnits;
      NicRespObj.PhaseUnits = NewUnit;
      if ~strcmpi(OldUnit,NewUnit)
         NicRespObj = newunits(NicRespObj,'phase',OldUnit);
      end

   case 'FrequencyUnits'
      if strncmpi(Value,'h',1)
         NewUnit = 'Hz';
      elseif strncmpi(Value,'r',1)
         NewUnit = 'rad/sec';
      else
         error(sprintf('Bad value for response property %s.',Property))
      end
      OldUnit = NicRespObj.FrequencyUnits;
      NicRespObj.FrequencyUnits = NewUnit;
      if ~strcmpi(OldUnit,NewUnit)
         NicRespObj = newunits(NicRespObj,'frequency',OldUnit);
      end

   case 'GridLines'
      NicRespObj.GridLines = Value;

   case 'PeakResponse'
      if isempty(strmatch(Value,{'on';'off'})) | ...
            length(strmatch(Value,{'on';'off'}))>1
         error(sprintf('Bad value for property %s.',Property))
      else
         NicRespObj.PeakResponse = Value;
         set(ContextMenu.PlotOptions.PeakResponse,'Checked',Value);
         NicRespObj = respfcn('showpeak',NicRespObj);
      end

   case 'StabilityMargin'
      if ~isfield(ContextMenu.PlotOptions,'StabilityMargin')
         error(sprintf('Property %s cannot be set for response plots',Property))
      else
         if isempty(strmatch(Value,{'on';'off'})) | ... 
               length(strmatch(Value,{'on';'off'}))>1
            error(sprintf('Bad value for property %s.',Property))
         else
            %---Turn off all margin display if turning on minimum margins
            if strcmpi(Value,'on')
               NicRespObj.AllStabilityMargin = 'off';
               set(ContextMenu.PlotOptions.AllStabilityMargin,'Checked','off');
               NicRespObj = respfcn('showallmargin',NicRespObj);
            end
            NicRespObj.StabilityMargin = Value;
            set(ContextMenu.PlotOptions.StabilityMargin,'Checked',Value);
            NicRespObj = respfcn('showmargin',NicRespObj);
         end
      end

   case 'AllStabilityMargin'
      if ~isfield(ContextMenu.PlotOptions,'AllStabilityMargin')
         error(sprintf('Property %s cannot be set for response plots',Property))
      else
         if isempty(strmatch(Value,{'on';'off'})) | ...
               length(strmatch(Value,{'on';'off'}))>1
            error(sprintf('Bad value for property %s.',Property))
         else
            %---Turn off minimum margin display if turning on all margins
            if strcmpi(Value,'on')
               NicRespObj.StabilityMargin = 'off';
               set(ContextMenu.PlotOptions.StabilityMargin,'Checked','off');
               NicRespObj = respfcn('showmargin',NicRespObj);  
            end
            NicRespObj.AllStabilityMargin = Value;
            set(ContextMenu.PlotOptions.AllStabilityMargin,'Checked',Value);
            NicRespObj = respfcn('showallmargin',NicRespObj);
         end
      end

   case 'Frequency'
      NicRespObj.Frequency = Value;

   case {'PeakResponseValue','StabilityMarginValue','AllStabilityMarginValue'}
      error(sprintf('Attempt to modify read-only property %s',Property))

   case 'Grid'
      NicRespObj = respfcn('setnicgrid',NicRespObj,Value);

   case 'UnwrapPhase'
      if isempty(strmatch(Value,{'on';'off'})) | ...
            length(strmatch(Value,{'on';'off'}))>1
         error(sprintf('Bad value for response property %s.',Property))
      else
         OldValue = NicRespObj.UnwrapPhase;
         NicRespObj.UnwrapPhase = Value;
         if ~strcmpi(OldValue,Value)
            NicRespObj = phasewrap(NicRespObj);
         end
      end

   otherwise
      % PROPERTY is a regular parent @response property.
      RespObjProp([i i+1]) = logical(1);

   end
end

% Set all @response properties
RespObjProp = find(RespObjProp);
if ~isempty(RespObjProp)
   NicRespObj.response = pvset(NicRespObj.response,varargin{RespObjProp});
end
