function varargout = rguiropts(varargin);
%RGUIROPTS Open the Response Preferences for the LTI Viewer
%   RGUIROPTS(action) contains the functions for opening the Response
%   Preferences window for the LTI Viewer, as well as the 
%   functions called by the controls located on the preference window
%
%   See also LTIVIEW

%   Karen Gondoly, 8-6-96
%   Copyright (c) 1986-1999 The Mathworks, Inc. All Rights Reserved.
%   $Revision: 1.12 $

ni=nargin;

%%%-Protection if file is called from the CommandLine
if isempty(gcbo) & ni==1,
   disp('WARNING: RGUIROPTS is not a Command Line function')
   return
end


switch ni
case 0, % called from a Response Preferences menu
   %%%-Look for an already open response preferences window
   RespPrefFig=get(gcbo,'UserData');
   LTIviewerFig=gcbf;
   
   if isempty(RespPrefFig) | ~RespPrefFig , 
      action='initialize';
   else
      %---Use only one response preference window for all GUIs
      action = 'visible';
   end % if/else ~RespPrefFig
   
case 1
   action=varargin{1};
   RespPrefFig = gcbf;
   
case 2
   action=varargin{1};
   LTIviewerFig=varargin{2};
   
end % if nargin

switch action
   
case 'initialize'
   %---Open the initial Response Preferences window
   RespGUI = LocalOpenFig(LTIviewerFig);
   
case 'visible',
   %---Make the open Response Preferencw window visible and bring it forward
   allmenus=findobj(LTIviewerFig,'Tag','RespMenu');
   RespGUI=get(allmenus,'UserData');
   set(RespGUI,'visible','on');
   figure(RespGUI)
   
case 'revert',
   LocalRevertData(RespPrefFig);
   
   %---Callback to handle mutually exclusive radio buttons
case 'radiocallback'
   val=get(gcbo,'Value');
   ud=get(gcbo,'UserData');
   
   if val, 
      set(ud(2:length(ud)),'Value',0);
      
      if strcmp(get(gcbo,'Tag'),'FreqHzButton') | strcmp(get(gcbo,'Tag'),'FreqRadButton'),
         LocalSetFreqText(gcbf,gcbo);
      end
      
      %---Check if an associated editable text box needs to be en/disabled
      if strcmp(get(ud(1),'type'),'uicontrol'),
         if ~isempty(strmatch('Define',get(gcbo,'String'))),
            set(ud(1),'Enable','on');
         else
            set(ud(1),'Enable','off');
         end
      end
      
      set(findobj(RespPrefFig,'Tag','ApplyButton'),'Enable','on');
   else
      set(gcbo,'value',1);
   end % if/else val
   
case 'deletefig'
   %---Delete function. Remove the Response Preference window from the
   %----LTI Viewer menu userdata
   ud = get(gcbf,'UserData');
   if ishandle(ud.Parent),
      RM = findobj(ud.Parent,'Tag','RespMenu');
      if ~isempty(RM),
         set(RM,'UserData',0);
      end
   end
   
case 'apply'
   %---Apply the new preferences
   %---Put the new values in the Revert userdata and look for what has changed
   set(findobj(RespPrefFig,'Tag','ApplyButton'),'Enable','off');
   ud = get(gcbf,'UserData');
   ViewerObj = get(ud.Parent,'UserData');
   ViewerObj = respapply(ViewerObj);
   
   %---Help function
case 'help'
   
   titleStr='LTI Viewer Help';
   
   str1={'The Response Preferences window allows you to customize some';
      'of the quantities used to generate the response plots, including:';
      ' ';
      '  1) The duration of time domain responses';
      '  2) The target for settling time calculations';
      '  3) The frequency range of frequency domain responses';
      '  4) The units of frequency domain response plots';
      ' ';
      'Each LTI Viewer has its own Response Preferences window.';
      ' ';
      'To open the Response Preferences window, either:';
      '  1) Choose "Response Preferences" from the Tools menu';
      '  2) Press CTRL-R on your keyboard';
      ' ';
     'To use the Response Preferences window, make your desired changes then:';
	  '  Press <OK> to apply any changes in the Response Preference window';
	  '         to the LTI Viewer and close the Response Preference window.';
	  '  Press <Cancel> to close the Response Preference window without';
	  '          saving the changes made since the last Apply or OK.';
	  '  Press <Help> to access the on-line help window, also accessible';
	  '         from the main LTI Viewer window';
	  '  Press <Apply> to apply any changes in the Response Preference window';
	  '         to the LTI Viewer without closing the Response Preference window.';
      ' ';
      'A word of caution: Any changes you make in this window will be';
      '  reflected in the LTI Viewer only after you have pressed Apply.';
      ' ';
      'Use the Topics popupmenu to view more Response Preferences help.'};  
   
   
   str2={'The Time Domain field allows you to set the following properties:';
      ' ';
      '  1) The time vector used when calculating any time domain responses';
      '  2) The target percentage used when calculating the settling time';
      ' ';
      'To set the time vector, select either:';
      '  <Generate automatically> to instruct MATLAB to set the time range';
      '  <Define> to set the time range yourself, as follows:';
      ' ';
      '  Enter three numbers, separated by colons, indicating:';
      '    1) Ti - The initial time of the response';
      '    2) Tf - The final time of the response';
      '    3) dt - The sample time of the system';
      ' ';
      '    For continuous systems, dt becomes the sample time of a';
      '    discrete approximation to the continuous system.';
      ' ';
      '    If only one number is entered, it is taken as Tf for continuous systems,';
      '       and the number of samples, for discrete systems';
      '    If two numbers are entered, dt is taken as 1';
      ' ';
      'To set the target percentage for settling time calculations:';
      '  Enter a numerical value from 0-100 in the associated editable text field.';
      '  A default value of 2% will be used if the Response Preferences window';
      '  has not been opened.'};
   
   str3={'The Frequency Domain field allows you to set the following properties:';
      ' ';
      '  1) The frequency vector used when calculating any frequency domain response';
      '  2) The units to use when displaying certain frequency domain plots';
      ' ';
      'To set the frequency vector, select either:';
      '  <Generate Automatically> to have MATLAB define a logarithmic frequency';
      '    Checking the ''Recalculate for each response type'' box causes MATLAB to';
      '      range choose a frequency vector that results in a smooth response plot';
      '      and recalculate each response using that vector.';
      '    Unchecking the box instructs MATLAB to simply convert any frequency data';
      '      that is currently plotted, whenever a new frequency response type is ';
      '      selected. This may be faster for large systems, but could result in ';
      '      poor response plots';
      '    This box only applies when switching between frequency domain plots.';
      '      When switching from time to frequency domain plots, responses are ';
      '      always recalculated';
      '';
      '  <Define> to set the logarithmic frequency range yourself, as follows:';
      ' ';
      '  Enter three numbers, separated by commas, indicating:';
      '    1) d1 - The exponent of 10 at which the frequency range starts';
      '    2) d2 - The exponent of 10 at which the frequency range ends';
      '    3) N  - The number of frequency points (optional)';
      ' ';
      '    If only two numbers are entered, N defaults to 50 points';
      ' ';
      'To set the response plot units, select the radio button associated';
      '  with the desired units. The default units are:';
      ' ';
      '  1) Magnitude: dB';
      '  2) Phase: degrees';
      '  3) Frequency: radians/second';
      ' ';
      '  Any changes made to the default units will not be reflected in Nichols';
      '  charts, in order to maintain a consistency with the ngrid'};
   
   helpwin({'Response Preferences',str1;'Time Domain',str2;'Frequency Domain',str3}, ...
      'Response Preferences',titleStr);
   
case 'recalcbox'
   %---Callback for Recalc... Checkbox
   set(findobj(RespPrefFig,'Tag','ApplyButton'),'Enable','on');
      
case 'checkedit'
%---Check on edit boxes
   RespFig = gcbf;
   udfig = get(RespFig,'UserData');
   T=gcbo;
   ud=get(T,'UserData');
   oldentry=ud(1);
   
   attempt=str2double(get(T,'String'));
   if isnan(attempt), % Not a number, or more then a scalar
      set(T,'String',num2str(oldentry));
   else
      %---Make sure the upper and lower percentages are increasing
      if length(ud)>1,
         set(T,'UserData',[attempt,ud(2)]);       
         otherlimit = str2double(get(ud(2),'String'));
         switch get(T,'Tag'),
         case 'RiseTimeStart',
            if attempt>=otherlimit,
               set(T,'String',num2str(oldentry),'UserData',[oldentry,ud(2)]);
            elseif attempt<0,
               set(T,'String',num2str(0),'UserData',[0,ud(2)]);               
            end
         case 'RiseTimeEnd',
            if attempt<=otherlimit,
               set(T,'String',num2str(oldentry),'UserData',[oldentry,ud(2)]);
            elseif attempt>100,
               set(T,'String',num2str(100),'UserData',[100,ud(2)]);
            end
         end, % switch get(T,'Tag')
      else
         set(T,'UserData',attempt)
      end, % if length(ud)
   end, % if/else isempty(attempt
   
   set(findobj(RespPrefFig,'Tag','ApplyButton'),'Enable','on');
   
case 'checktimeedit'
   %---Check on time vector edit box
   T=gco;
   
   Tstr=deblank(get(T,'String'));
   oldentry=get(T,'UserData');
   
   if isempty(Tstr), %  empty
      set(T,'String',oldentry);
      
   else
      %---Remove any blanks at the beginning of the string
      indT=find(~isspace(Tstr));
      Tstr=Tstr(indT(1):end);
      
      %----Error checking for brackets on string
      if ~strcmp(Tstr(1),'[');
         Tstr=['[',Tstr];
      end
      
      if ~strcmp(Tstr(length(Tstr)),']');
         Tstr=[Tstr,']'];
      end
      
      attempt=eval(Tstr,'[]');
      
      if isempty(attempt), % Tstr was not a number
         set(T,'String',oldentry);
      else	
         
         %---Make sure the time vector is increasing
         if attempt(end)<attempt(1), % not increasing, switch to old value
            set(T,'String',oldentry);
            
         else
            % Make sure colons separate the entries 
            newstr=['[',num2str(attempt(1))];
            if length(attempt)==1,
               newstr=[newstr,']'];
            elseif length(attempt)==2,
               newstr=[newstr,':',num2str(attempt(2)),']'];
            else
               dt=attempt(3)-attempt(2);
               newstr=[newstr,':',num2str(dt),':',num2str(attempt(end)),']'];
            end % if/else length(attempt)
            set(T,'UserData',newstr,'String',newstr);       
         end % if/else attempt(end)...
      end
      
   end % if/else isempty(Tstr)
   set(findobj(RespPrefFig,'Tag','ApplyButton'),'Enable','on');
   
case 'checkylimedit'
   %---Check on y-limit vector edit box
   Y=gco;
   
   Ystr=deblank(get(Y,'String'));
   oldentry=get(Y,'UserData');
   
   if isempty(Ystr), %  empty
      set(Y,'String',oldentry);
      
   else
      %---Remove any blanks at the beginning of the string
      indY=find(~isspace(Ystr));
      Ystr=Ystr(indY(1):end);
      
      %----Error checking for brackets on string
      if ~strcmp(Ystr(1),'[');
         Ystr=['[',Ystr];
      end
      
      if ~strcmp(Ystr(length(Ystr)),']');
         Ystr=[Ystr,']'];
      end
      
      attempt=eval(Ystr,'[]');
      
      if isempty(attempt), % Ystr was not a number
         set(Y,'String',oldentry);
      elseif length(attempt)~=2,
         set(Y,'String',oldentry); 
      else	
         
         %---Make sure the time vector is increasing
         if attempt(end)<attempt(1), % not increasing, switch to old value
            set(Y,'String',oldentry);  
         else
            % Make sure comma separate the entries 
            newstr=['[',num2str(attempt(1)),',',num2str(attempt(2)),']'];
            set(Y,'UserData',newstr,'String',newstr);       
         end % if/else length(attempt)
      end % if/else attempt(end)...
      
   end % if/else isempty(Ystr)
   set(findobj(RespPrefFig,'Tag','ApplyButton'),'Enable','on');
   
case 'checkfreqedit'
   %---Check on freq vector edit box
   W=gco;
   
   Wstr=deblank(get(W,'String'));
   oldentry=get(W,'UserData');
   
   if isempty(Wstr), %  empty
      set(W,'String',oldentry);
      
   else
      %---Remove any blanks at the beginning of the string
      indW=find(~isspace(Wstr));
      Wstr=Wstr(indW(1):end);
      
      %----Error checking for paren. on string
      if ~strcmp(Wstr(1),'(');
         Wstr=['(',Wstr];
      end
      
      if ~strcmp(Wstr(length(Wstr)),')');
         Wstr=[Wstr,')'];
      end
      
      attempt=eval(['[',Wstr(2:length(Wstr)-1),']'],'[]');
      
      if (length(attempt)<2) | (length(attempt)>3), % Need an initial and final value (N is optional)
         set(W,'String',oldentry);
      else
         
         %---Make sure the frequency is increasing
         if attempt(2)<attempt(1)
            set(W,'String',oldentry);
         else
            %---Make sure commas separate the entries (if 2 or 3 entries are given)
            newstr=['(',num2str(attempt(1)),',',num2str(attempt(2))];
            if length(attempt)==3,
               newstr=[newstr,',',num2str(attempt(3)),')'];
            else
               newstr=[newstr,')'];
            end 
            set(W,'UserData',newstr,'String',newstr);       
         end % if/else length(attempt)
      end % if/else attempt(2)...
   end % if/else isempty(Wstr)
   
   set(findobj(RespPrefFig,'Tag','ApplyButton'),'Enable','on');
end % switch action

if nargout,
   varargout{1}=RespGUI;
end

%---------------------------Internal Functions---------------------------

%%%%%%%%%%%%%%%%%%%%%%%
%%% LocalRevertData %%%
%%%%%%%%%%%%%%%%%%%%%%%
function LocalRevertData(RespPrefFig);
%---Revert to the values entered after the last Apply
%---Place original control values in their userdata 

ud = get(RespPrefFig,'UserData');

set(ud.Handles.DefaultTime,'Value',ud.Revert.DefaultTime);
set(ud.Handles.DefineTime,'Value',ud.Revert.DefineTime);
if ud.Revert.DefineTime,
   set(ud.Handles.TimeVector,'Enable','on');
else
   set(ud.Handles.TimeVector,'Enable','off');
end
set(ud.Handles.TimeVector,'String',ud.Revert.TimeVector);
set(ud.Handles.DefaultYrange,'Value',ud.Revert.DefaultYrange);
set(ud.Handles.DefineYrange,'Value',ud.Revert.DefineYrange);
if ud.Revert.DefineYrange,
   set(ud.Handles.YlimVector,'Enable','on');
else
   set(ud.Handles.YlimVector,'Enable','off');
end
set(ud.Handles.YlimVector,'String',ud.Revert.YlimVector);
set(ud.Handles.SetTimeTarget,'String',ud.Revert.SetTimeTarget);
set(ud.Handles.RiseTimeStart,'String',ud.Revert.RiseTimeStart);
set(ud.Handles.RiseTimeEnd,'String',ud.Revert.RiseTimeEnd);
set(ud.Handles.DefaultFreq,'Value',ud.Revert.DefaultFreq);
set(ud.Handles.RecalcFreq,'Value',ud.Revert.RecalcFreq);
set(ud.Handles.DefineFreq,'Value',ud.Revert.DefineFreq);
if ud.Revert.DefineFreq,
   set(ud.Handles.FreqVector,'Enable','on');
else
   set(ud.Handles.FreqVector,'Enable','off');
end
set(ud.Handles.FreqVector,'String',ud.Revert.FreqVector);
set(ud.Handles.MagdB,'Value',ud.Revert.MagdB);
set(ud.Handles.MagAbs,'Value',ud.Revert.MagAbs);
set(ud.Handles.MagLog,'Value',ud.Revert.MagLog);
set(ud.Handles.PhaseRad,'Value',ud.Revert.PhaseRad);
set(ud.Handles.PhaseDeg,'Value',ud.Revert.PhaseDeg);
set(ud.Handles.FreqHz,'Value',ud.Revert.FreqHz);
set(ud.Handles.FreqRad,'Value',ud.Revert.FreqRad);

if ud.Revert.FreqHz, % Changing to Hertz
   LocalSetFreqText(RespPrefFig,ud.Handles.FreqHz,1)
elseif ud.Revert.FreqRad, % Chaging to Rad/sec
   LocalSetFreqText(RespPrefFig,ud.Handles.FreqRad,1)
end   

set(findobj(RespPrefFig,'Tag','ApplyButton'),'Enable','off');

%%%%%%%%%%%%%%%%%%%%%%%%
%%% LocalSetFreqText %%%
%%%%%%%%%%%%%%%%%%%%%%%%
function LocalSetFreqText(varargin);

ni=nargin;
RespFig=varargin{1};
FreqButton=varargin{2};

if ni==2
   OverRideFreqFac=0;
else
   OverRideFreqFac=varargin{3};
end

%---See if the Hertz button is being pressed
udFig = get(RespFig,'UserData');
FreqText = findobj(RespFig,'Tag','FreqText');
FreqFac=[];

if strcmp(get(FreqButton,'Tag'),'FreqHzButton') & ...
      ~strcmp(get(FreqText,'String'),'Frequency vector (Hz.)'),
   %---Change frequency to Hertz
   set(FreqText,'String','Frequency vector (Hz.)');
   FreqFac = 1/(2*pi);
   
elseif strcmp(get(FreqButton,'Tag'),'FreqRadButton') & ...
      ~strcmp(get(FreqText,'String'),'Frequency vector (rad/sec.)'),
   %---Change frequency to rad/sec
   set(FreqText,'String','Frequency vector (rad/sec.)');
   FreqFac=2*pi;
end

if ~isempty(FreqFac),
   if OverRideFreqFac,
      FreqFac=1;
   end
   FreqStr = get(udFig.Handles.FreqVector,'String');
   indcomma = findstr(',',FreqStr);
   NewFreqStr = ['(',num2str(log10(10^(str2double(FreqStr(2:indcomma(1)-1)))*FreqFac),'%3.1f'),','];
   if length(indcomma)>1,
      NewFreqStr =[NewFreqStr,...
            num2str(log10(10^(str2double(FreqStr(indcomma(1)+1:indcomma(2)-1)))*FreqFac),'%3.1f'),...
            FreqStr(indcomma(2):end)];
   else
      NewFreqStr=[NewFreqStr,...
            num2str(log10(10^(str2double(FreqStr(indcomma(1)+1:end-1)))*FreqFac),'%3.1f'),')'];
   end      
   
   set(udFig.Handles.FreqVector,'String',NewFreqStr,'UserData',NewFreqStr);
   set(RespFig,'UserData',udFig)
end

%%%%%%%%%%%%%%%%%%%%
%%% LocalOpenFig %%%
%%%%%%%%%%%%%%%%%%%%
function a = LocalOpenFig(LTIviewerFig);

%%%-Find any currently displayed time or frequency range limits
ViewerObj = get(LTIviewerFig,'UserData');
AllProps = get(ViewerObj);

udP = struct('Parent',LTIviewerFig,'Handles',[],'Revert',[]);

if strcmpi(AllProps.TimeVectorMode,'manual')
   TradVals = [0;1];
   trange = AllProps.TimeVector;
else
   TradVals = [1;0];
   trange=[0:0.2:20];
end

if strcmpi(AllProps.FrequencyVectorMode,'manual')
   if iscell(AllProps.FrequencyVector)
      startW=chop(log10(AllProps.FrequencyVector{1}),3);
      endW = chop(log10(AllProps.FrequencyVector{2}),3);
      numW=50;
   else
      numW=length(AllProps.FrequencyVector);
      if ~AllProps.FrequencyVector(1), % First frequency is zero, use second to find range
         Wtemp=AllProps.FrequencyVector(2);
      else
         Wtemp=AllProps.FrequencyVector(1);
      end
      startW=chop(log10(Wtemp),3);
      
      endW=chop(log10(AllProps.FrequencyVector(end)),3);
      if ~isfinite(startW),
         endW=2; % Default 
      end
   end % if/else iscell(...)
   WradVals = [0;1];
else
   WradVals = [1;0];
   startW = -2;
   endW = 2;
   numW = 50;
end % if/else strcmpi(FrequencyVectorMode,'manual')

%---Get the time or frequency range of the first time/frequency response

if isequal(length(trange),1)
   T = num2str(trange);
else
   dt=chop(trange(end)/(length(trange)-1),2);
   T=['[',num2str(chop(trange(1),3)),':',num2str(dt),':',num2str(chop(trange(end),3)),']'];
end

W=['(',num2str(startW),',',num2str(endW),',',num2str(numW),')'];

%---Get the frequency domain units
allMagUnit = {'decibels';'absolute';'logarithmic'};
MagVals = strcmpi(AllProps.MagnitudeUnits,allMagUnit);
allPhUnit = {'radians';'degrees'};
PhVals = strcmpi(AllProps.PhaseUnits,allPhUnit);
allFreqUnit = {'h';'r'};
FreqVals = strncmpi(AllProps.FrequencyUnits,allFreqUnit,1);

%%%-Make initial figure-%%%
figurepos=[20, 35 455 260];
StdUnit = 'points';
StdColor = get(0,'DefaultUicontrolBackgroundColor');

a = figure('Color',[0.8 0.8 0.8], ...
   'Name','Response Preferences', ...
   'MenuBar','none', ...
   'NumberTitle','off', ...
   'Resize','off', ...
   'Integer','off', ...
   'HandleVisibility','callback', ...
   'DeleteFcn','rguiropts(''deletefig'');', ...
   'Unit',StdUnit, ...
   'Position',figurepos, ...
   'Tag','RespPrefs');

%%%-Put the figure number in the response options menu
ViewerKids = findobj(LTIviewerFig,'Tag','ResponseGUI');
set(findobj(ViewerKids,'Tag','RespMenu'),'UserData',a);

%%%-Window Options-%%%
b = uicontrol('Parent',a, ...
   'Units',StdUnit, ...
   'Position',[11 6 73 20], ...
   'String','OK', ...
   'Callback','rguiropts(''apply'');set(gcbf,''visible'',''off'');', ...
   'Tag','OKButton');
b = uicontrol('Parent',a, ...
   'Units',StdUnit, ...
   'Position',[132 6 73 20], ...
   'String','Cancel', ...
   'Callback','rguiropts(''revert'');set(gcbf,''visible'',''off'');', ...
   'Tag','CloseButton');
b = uicontrol('Parent',a, ...
   'Units',StdUnit, ...
   'Position',[254 6 73 20], ...
   'String','Help', ...
   'Callback','rguiropts(''help'');', ...
   'Tag','HelpButton');
b = uicontrol('Parent',a, ...
   'Units',StdUnit, ...
   'Position',[377 6 73 20], ...
   'String','Apply', ...
   'Callback','rguiropts(''apply'');', ...
   'Enable','off', ...
   'Tag','ApplyButton');

%%%-Add frames-%%%

b = uicontrol('Parent',a, ...
   'Units',StdUnit, ...
   'Position',[13 37 201 211], ...
   'BackgroundColor',StdColor-0.02, ...
   'Style','frame');
b = uicontrol('Parent',a, ...
   'Units',StdUnit, ...
   'BackgroundColor',StdColor-0.02, ...
   'Position',[222 37 227 211], ...
   'Style','frame');
b = uicontrol('Parent',a, ...
   'Units',StdUnit, ...
   'BackgroundColor',StdColor , ...
   'Position',[19 180 187 50], ...
   'Style','frame');
b = uicontrol('Parent',a, ...
   'Units',StdUnit, ...
   'BackgroundColor',StdColor, ...
   'Position',[19 115 187 60], ...
   'Style','frame');
b = uicontrol('Parent',a, ...
   'Units',StdUnit, ...
   'BackgroundColor',StdColor, ...
   'Position',[19 45 185 65], ...
   'Style','frame');
b = uicontrol('Parent',a, ...
   'Units',StdUnit, ...
   'BackgroundColor',StdColor, ...
   'Position',[231 166 209 66], ...
   'Style','frame');
b = uicontrol('Parent',a, ...
   'Units',StdUnit, ...
   'BackgroundColor',StdColor, ...
   'Position',[231 48 209 114], ...
   'Style','frame');

%%%-Add time domain preferences
b = uicontrol('Parent',a, ...
   'Units',StdUnit, ...
   'BackgroundColor',StdColor-0.02, ...
   'Position',[72 233 81 14], ...
   'String','Time Domain', ...
   'Style','text');

%---Time Duration controls
b = uicontrol('Parent',a, ...
   'Units',StdUnit, ...
   'BackgroundColor',StdColor, ...
   'HorizontalAlignment','center', ...
   'Position',[48 213 121 14], ...
   'Tag','TimeText', ...
   'UserData',T, ...
   'String','Time vector (sec.)', ...
   'Style','text');
udP.Handles.DefaultTime = uicontrol('Parent',a, ...
   'Units',StdUnit, ...
   'BackgroundColor',StdColor, ...
   'Position',[25 200 127 17],...
   'Style','radio', ...
   'Horiz','left', ...
   'Value',TradVals(1), ...
   'Callback','rguiropts(''radiocallback'');', ...
   'Tag','DefaultTime', ...
   'String','Generate automatically');
udP.Revert.DefaultTime=TradVals(1);
udP.Handles.DefineTime = uicontrol('Parent',a, ...
   'Units',StdUnit, ...
   'BackgroundColor',StdColor, ...
   'Position',[25 185 127 17],...
   'Style','radio', ...
   'Callback','rguiropts(''radiocallback'');', ...
   'Horiz','left', ...
   'Tag','DefineTime', ...
   'Value',TradVals(2), ...
   'String','Define: [Ti:dt:Tf]');
udP.Revert.DefineTime = TradVals(2);
udP.Handles.TimeVector = uicontrol('Parent',a, ...
   'Units',StdUnit, ...
   'BackgroundColor',[1 1 1], ...
   'Position',[152 185 50 20], ...
   'Style','edit', ...
   'String',T, ...
   'Enable','off', ...
   'Callback','rguiropts(''checktimeedit'');', ...
   'horiz','left', ...
   'UserData',T, ...
   'Tag','TimeVec');
if TradVals(2),
   set(udP.Handles.TimeVector,'Enable','on')
end
udP.Revert.TimeVector=T;

set(udP.Handles.DefaultTime,'UserData',[udP.Handles.TimeVector,udP.Handles.DefineTime]);
set(udP.Handles.DefineTime,'UserData',[udP.Handles.TimeVector,udP.Handles.DefaultTime]);

%---Y-range controls
b = uicontrol('Parent',a, ...
   'Units',StdUnit, ...
   'BackgroundColor',StdColor, ...
   'HorizontalAlignment','center', ...
   'Position',[48 157 121 14], ...
   'Tag','YText', ...
   'UserData','[0, 1]', ...
   'String','Y-axis Range', ...
   'Style','text');
udP.Handles.DefaultYrange = uicontrol('Parent',a, ...
   'Units',StdUnit, ...
   'BackgroundColor',StdColor, ...
   'Position',[25 140 132 17],...
   'Style','radio', ...
   'Horiz','left', ...
   'Value',1, ...
   'Callback','rguiropts(''radiocallback'');', ...
   'Tag','DefaultYrange', ...
   'String','Generate automatically');
udP.Revert.DefaultYrange = 1;
udP.Handles.DefineYrange = uicontrol('Parent',a, ...
   'Units',StdUnit, ...
   'BackgroundColor',StdColor, ...
   'Position',[25 125 132 17],...
   'Style','radio', ...
   'Horiz','left', ...
   'Callback','rguiropts(''radiocallback'');', ...
   'Tag','DefineYrange', ...
   'String','Define: [Ymin,Ymax]');
udP.Revert.DefineYrange = 0;
udP.Handles.YlimVector = uicontrol('Parent',a, ...
   'Units',StdUnit, ...
   'BackgroundColor',[1 1 1], ...
   'Position',[160 125 40 20], ...
   'Style','edit', ...
   'Enable','off', ...
   'String','[0, 1]', ...
   'Callback','rguiropts(''checkylimedit'');', ...
   'Horiz','left', ...
   'UserData','[0, 1]', ...
   'Tag','YlimVec');
udP.Revert.YlimVector = '[0, 1]';

set(udP.Handles.DefaultYrange,'UserData',[udP.Handles.YlimVector,udP.Handles.DefineYrange]);
set(udP.Handles.DefineYrange,'UserData',[udP.Handles.YlimVector,udP.Handles.DefaultYrange]);


%---Settling time specification
b = uicontrol('Parent',a, ...
   'Units',StdUnit, ...
   'BackgroundColor',StdColor, ...
   'Position',[65 92 81 14], ...
   'String','Plot Option', ...
   'Style','text');

%---Settling Time controls
b = uicontrol('Parent',a, ...
   'Units',StdUnit, ...
   'BackgroundColor',StdColor, ...
   'Position',[25 78 120 14], ...
   'String','Settling time within:', ...
   'horiz','left', ...
   'Userdata',AllProps.SettlingTimeThreshold*100, ...
   'Tag','SetText', ...
   'Style','text');
udP.Handles.SetTimeTarget = uicontrol('Parent',a, ...
   'Units',StdUnit, ...
   'BackgroundColor',[1 1 1], ...
   'horiz','left', ...
   'Position',[155 78 30 20], ...
   'String',num2str(AllProps.SettlingTimeThreshold*100), ...
   'Callback','rguiropts(''checkedit'');', ...
   'Tag','SetTimeTarget', ...
   'UserData',AllProps.SettlingTimeThreshold*100, ...
   'Style','edit');
udP.Revert.SetTimeTarget = num2str(AllProps.SettlingTimeThreshold*100);
b = uicontrol('Parent',a, ...
   'Units',StdUnit, ...
   'BackgroundColor',StdColor, ...
   'Position',[190 78 12 14], ...
   'String','%', ...
   'horiz','left', ...
   'Style','text');

%---Rise Time controls
b = uicontrol('Parent',a, ...
   'Units',StdUnit, ...
   'BackgroundColor',StdColor, ...
   'Position',[25 50 80 14], ...
   'String','Rise time from:', ...
   'horiz','left', ...
   'Tag','RiseText', ...
   'Style','text');
udP.Handles.RiseTimeStart = uicontrol('Parent',a, ...
   'Units',StdUnit, ...
   'BackgroundColor',[1 1 1], ...
   'horiz','left', ...
   'Position',[110 50 30 20], ...
   'String',num2str(AllProps.RiseTimeLimits(1)*100), ...
   'Callback','rguiropts(''checkedit'');', ...
   'Tag','RiseTimeStart', ...
   'Style','edit');
udP.Revert.RiseTimeStart = num2str(AllProps.RiseTimeLimits(1)*100);
b = uicontrol('Parent',a, ...
   'Units',StdUnit, ...
   'BackgroundColor',StdColor, ...
   'Position',[144 50 10 14], ...
   'String','to', ...
   'horiz','left', ...
   'Tag','ToText', ...
   'Style','text');
udP.Handles.RiseTimeEnd = uicontrol('Parent',a, ...
   'Units',StdUnit, ...
   'BackgroundColor',[1 1 1], ...
   'horiz','left', ...
   'Position',[155 50 30 20], ...
   'String',num2str(AllProps.RiseTimeLimits(2)*100), ...
   'Callback','rguiropts(''checkedit'');', ...
   'Tag','RiseTimeEnd', ...
   'Style','edit');
udP.Revert.RiseTimeEnd = num2str(AllProps.RiseTimeLimits(2)*100);
set(udP.Handles.RiseTimeStart,'UserData',[AllProps.RiseTimeLimits(1)*100,udP.Handles.RiseTimeEnd]);
set(udP.Handles.RiseTimeEnd,'UserData',[AllProps.RiseTimeLimits(2)*100,udP.Handles.RiseTimeStart]);

b = uicontrol('Parent',a, ...
   'Units',StdUnit, ...
   'BackgroundColor',StdColor, ...
   'Position',[190 50 12 14], ...
   'String','%', ...
   'horiz','left', ...
   'Style','text');

%%%-Add frequency domain constraints-%%%

b = uicontrol('Parent',a, ...
   'Units',StdUnit, ...
   'BackgroundColor',StdColor-0.02, ...
   'Position',[270 233 125 14], ...
   'String','Frequency Domain', ...
   'Style','text');

%---Frequency response duration controls
b = uicontrol('Parent',a, ...
   'Units',StdUnit, ...
   'BackgroundColor',StdColor, ...
   'HorizontalAlignment','center', ...
   'Position',[261 215 162 14], ...
   'Tag','FreqText', ...
   'UserData',W, ...
   'String','Frequency vector (rad/sec.)', ...
   'Style','text');
udP.Handles.DefaultFreq = uicontrol('Parent',a, ...
   'Units',StdUnit, ...
   'BackgroundColor',StdColor, ...
   'Position',[234 202 142 17],...
   'Style','radio', ...
   'Horiz','left', ...
   'Value',WradVals(1), ...
   'Callback','rguiropts(''radiocallback'');', ...
   'Tag','DefaultFreq', ...
   'String','Generate automatically');
udP.Revert.DefaultFreq = WradVals(1);
udP.Handles.RecalcFreq = uicontrol('Parent',a, ...
   'Units',StdUnit, ...
   'BackgroundColor',StdColor, ...
   'Position',[250 188 185 15],...
   'Style','check', ...
   'Horiz','left', ...
   'Value',1, ...
   'Callback','rguiropts(''recalcbox'');', ...
   'Tag','RecalcFreq', ...
   'String','Recalculate for each response type');
udP.Revert.RecalcFreq = 1;
udP.Handles.DefineFreq = uicontrol('Parent',a, ...
   'Units',StdUnit, ...
   'BackgroundColor',StdColor, ...
   'Position',[234 169 145 17],...
   'Callback','rguiropts(''radiocallback'');', ...
   'Style','radio', ...
   'Horiz','left', ...
   'Tag','DefineFreq', ...
   'Value',WradVals(2), ...
   'String','Define: logspace(d1,d2,N)');
udP.Revert.DefineFreq = WradVals(2);
udP.Handles.FreqVector = uicontrol('Parent',a, ...
   'Units',StdUnit, ...
   'BackgroundColor',[1 1 1], ...
   'horiz','left', ...
   'Position',[380 169 55 20], ...
   'Style','edit', ...
   'UserData',W, ...
   'Enable','off', ...
   'Callback','rguiropts(''checkfreqedit'');', ...
   'String',W, ...
   'Tag','FreqVec');
if WradVals(2),
   set(udP.Handles.FreqVector,'Enable','on');
end
udP.Revert.FreqVector = W;

set(udP.Handles.DefaultFreq,'UserData',[udP.Handles.FreqVector,udP.Handles.DefineFreq]);
set(udP.Handles.DefineFreq,'UserData',[udP.Handles.FreqVector,udP.Handles.DefaultFreq]);

%---Magnitude unit controls
b = uicontrol('Parent',a, ...
   'Units',StdUnit, ...
   'BackgroundColor',StdColor, ...
   'HorizontalAlignment','left', ...
   'Position',[239 130 81 14], ...
   'String','Magnitude in:', ...
   'Style','text');
udP.Handles.MagdB = uicontrol('Parent',a, ...
   'Units',StdUnit, ...
   'BackgroundColor',StdColor, ...
   'Position',[327 142 97 17], ...
   'String',' Decibels', ...
   'horiz','left', ...
   'Callback','rguiropts(''radiocallback'');', ...
   'Style','radiobutton', ...
   'Tag','dBbutton', ...
   'Value',MagVals(1));
udP.Revert.MagdB = MagVals(1);
udP.Handles.MagAbs = uicontrol('Parent',a, ...
   'Units',StdUnit, ...
   'BackgroundColor',StdColor, ...
   'Position',[327 127 97 17], ...
   'horiz','left', ...
   'String',' Absolute', ...
   'Callback','rguiropts(''radiocallback'');', ...
   'Style','radiobutton', ...
   'Tag','AbsButton',...
   'Value',MagVals(2));
udP.Revert.MagAbs = MagVals(2);
udP.Handles.MagLog = uicontrol('Parent',a, ...
   'Units',StdUnit, ...
   'BackgroundColor',StdColor, ...
   'Position',[327 112 97 17], ...
   'horiz','left', ...
   'Callback','rguiropts(''radiocallback'');', ...
   'String',' Logarithmic', ...
   'Style','radiobutton', ...
   'Tag','LogButton',...
   'Value',MagVals(3));
udP.Revert.MagLog = MagVals(3);

%---Set Mag. userdata
set(udP.Handles.MagdB,'UserData',[0, udP.Handles.MagAbs,udP.Handles.MagLog]);
set(udP.Handles.MagAbs,'UserData',[0, udP.Handles.MagdB,udP.Handles.MagLog]);
set(udP.Handles.MagLog,'UserData',[0, udP.Handles.MagdB,udP.Handles.MagAbs]);

%---Phase Unit controls
b = uicontrol('Parent',a, ...
   'Units',StdUnit, ...
   'BackgroundColor',StdColor, ...
   'HorizontalAlignment','left', ...
   'Position',[242 88 81 14], ...
   'String','Phase in:', ...
   'Style','text');
udP.Handles.PhaseRad = uicontrol('Parent',a, ...
   'Units',StdUnit, ...
   'BackgroundColor',StdColor, ...
   'Position',[327 96 97 17], ...
   'String',' Radians', ...
   'Style','radiobutton', ...
   'Callback','rguiropts(''radiocallback'');', ...
   'horiz','left', ...
   'Value',PhVals(1), ...
   'Tag','PhRadButton');
udP.Revert.PhaseRad = 0;
udP.Handles.PhaseDeg = uicontrol('Parent',a, ...
   'Units',StdUnit, ...
   'BackgroundColor',StdColor, ...
   'Position',[327 81 97 17], ...
   'horiz','left', ...
   'Callback','rguiropts(''radiocallback'');', ...
   'String',' Degrees', ...
   'Style','radiobutton', ...
   'Tag','PhDegButton', ...
   'Value',PhVals(2));
udP.Revert.PhaseDeg = 1;

%---Set Phase userdata
set(udP.Handles.PhaseRad,'UserData',[0,udP.Handles.PhaseDeg]);
set(udP.Handles.PhaseDeg,'UserData',[0,udP.Handles.PhaseRad]);

%---Frequency unit controls
b = uicontrol('Parent',a, ...
   'Units',StdUnit, ...
   'BackgroundColor',StdColor, ...
   'HorizontalAlignment','left', ...
   'Position',[242 60 81 14], ...
   'String','Frequency in:', ...
   'Style','text');
udP.Handles.FreqHz = uicontrol('Parent',a, ...
   'Units',StdUnit, ...
   'BackgroundColor',StdColor, ...
   'Position',[327 51 97 17], ...
   'horiz','left', ...
   'Callback','rguiropts(''radiocallback'');', ...
   'String',' Hertz', ...
   'Style','radiobutton', ...
   'Tag','FreqHzButton', ...
   'Value',FreqVals(1));
udP.Revert.FreqHz = 0;
udP.Handles.FreqRad = uicontrol('Parent',a, ...
   'Units',StdUnit, ...
   'BackgroundColor',StdColor, ...
   'Position',[327 66 97 17], ...
   'horiz','left', ...
   'Callback','rguiropts(''radiocallback'');', ...
   'String',' Radians/second', ...
   'Style','radiobutton', ...
   'Tag','FreqRadButton', ...
   'Value',FreqVals(2));
udP.Revert.FreqRad = 1;

%---Set Frequency userdata
set(udP.Handles.FreqHz,'UserData',[0,udP.Handles.FreqRad]);
set(udP.Handles.FreqRad,'UserData',[0,udP.Handles.FreqHz]);

set(a,'UserData',udP)
