function BodeRespObj = pvset(BodeRespObj,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): P. Gahinet
%              K. Gondoly
%              A. DiVergilio
%   Copyright 1986-2000 The MathWorks, Inc. 
%   $Revision: 1.21 $ $Date: 2000/07/18 19:47:09 $

ni = nargin;
ContextMenu = get(BodeRespObj.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 = BodeRespObj.MagnitudeUnits;
		BodeRespObj.MagnitudeUnits = NewUnit;
		if ~strcmpi(OldUnit,NewUnit)
			BodeRespObj = newunits(BodeRespObj,'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 = BodeRespObj.PhaseUnits;
		BodeRespObj.PhaseUnits = NewUnit;
		if ~strcmpi(OldUnit,NewUnit)
			BodeRespObj = newunits(BodeRespObj,'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 = BodeRespObj.FrequencyUnits;
		BodeRespObj.FrequencyUnits = NewUnit;
		if ~strcmpi(OldUnit,NewUnit)
			BodeRespObj = newunits(BodeRespObj,'frequency',OldUnit);
		end
		
	case 'PeakResponse'
		if isempty(strmatch(Value,{'on';'off'})) | ...
				length(strmatch(Value,{'on';'off'}))>1
			error(sprintf('Bad value for response property %s.',Property))
		else
			BodeRespObj.PeakResponse = Value;
			set(ContextMenu.PlotOptions.PeakResponse,'Checked',Value);
			BodeRespObj = respfcn('showpeak',BodeRespObj);
		end
		
	case 'StabilityMargin'
		InViewer = isfield(ContextMenu.PlotOptions,'StabilityMargin');
		if isempty(strmatch(Value,{'on';'off'})) | ... 
				length(strmatch(Value,{'on';'off'}))>1
			error(sprintf('Bad value for response property %s.',Property))
		else
			%---Turn off all margin display if turning on minimum margins
			if strcmpi(Value,'on')
				BodeRespObj.AllStabilityMargin = 'off';
				if InViewer,
					set(ContextMenu.PlotOptions.AllStabilityMargin,'Checked','off');
				end
				BodeRespObj = respfcn('showallmargin',BodeRespObj);
			end
			BodeRespObj.StabilityMargin = Value;
			if InViewer,
				set(ContextMenu.PlotOptions.StabilityMargin,'Checked',Value);
			end
			BodeRespObj = respfcn('showmargin',BodeRespObj);
		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 response property %s.',Property))
			else
				%---Turn off minimum margin display if turning on all margins
				if strcmpi(Value,'on')
					BodeRespObj.StabilityMargin = 'off';
					set(ContextMenu.PlotOptions.StabilityMargin,'Checked','off');
					BodeRespObj = respfcn('showmargin',BodeRespObj);
				end
				BodeRespObj.AllStabilityMargin = Value;
				set(ContextMenu.PlotOptions.AllStabilityMargin,'Checked',Value);
				BodeRespObj = respfcn('showallmargin',BodeRespObj);
			end
		end
		
	case 'StabilityMarginValue'
		% Hack for MARGINDEMO (temp)
		BodeRespObj.StabilityMarginValue = Value;
		
	case {'PeakResponseValue','AllStabilityMarginValue'}
		error(sprintf('Attempt to modify read-only property %s',Property))
		
	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 = BodeRespObj.UnwrapPhase;
			BodeRespObj.UnwrapPhase = Value;
			if ~strcmpi(OldValue,Value)
				BodeRespObj = phasewrap(BodeRespObj);
			end
		end
		
	otherwise
		% PROPERTY is a parent @response property. 
		RespObjProp([i i+1]) = logical(1);
	end
end

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