function respdemo
%RESPDEMO  Model Analysis Commands Demo.
%
%   RESPDEMO uses the commands available in the Control System
%   Toolbox to view the transient and frequency responses of
%   linear time-invariant (LTI) models.
%
%   Nine basic response types are available for analysis:
%
%       * Step
%       * Impulse
%       * Bode
%       * Bode Magnitude
%       * Nyquist
%       * Nichols
%       * Sigma
%       * Pole-Zero
%       * Root Locus
%
%   Each time a response is selected, a random 3rd-order SISO
%   state-space model is generated and the corresponding
%   response plot is displayed.
%
%   Each response plot contains a right-click menu for
%   accessing plot properties and response characteristics.
%
%   Left-clicking on the response curves will create moveable
%   data markers which can be used to view detailed response
%   information at specific time/frequency points.

%   Author(s): A. DiVergilio
%   Copyright 1986-2004 The MathWorks, Inc.
%   $Revision: 1.4.4.2 $  $Date: 2004/04/10 23:14:00 $

%---Open a new figure with single axes
 fg = figure(...
    'Name','Model Analysis Commands',...
    'IntegerHandle','off',...
    'NumberTitle','off',...
    'DoubleBuffer','on',...
    'DefaultUIControlUnits','normalized',...
    'DefaultAxesPosition',[0.100 0.425 0.635 0.50],...
    'HandleVisibility','callback',...
    'Visible','off',...
    'DockControls', 'off');

 ax = axes('Parent',fg,'Box','on');

%---Command text field
 top = 0.30;
 left = 0.05;
 right = 0.75;
 bottom = 0.05;
 frmBorder = 0.02;
 frmPos = [left-frmBorder bottom-frmBorder (right-left)+2*frmBorder (top-bottom)+2*frmBorder];
 uicontrol(...
    'Parent',fg,...
    'Style','frame',...
    'Position',frmPos,...
    'BackgroundColor',[0.50 0.50 0.50]);
 promptStr = str2mat(...
    ' Press the buttons at the right to see a demonstration of ',...
    ' the commands available in the Control System Toolbox for ',...
    ' analyzing linear time-invariant model responses.');
 TextField = uicontrol(...
    'Parent',fg,...
    'Style','text',...
    'HorizontalAlignment','left',...
    'Max',10,...
    'BackgroundColor',[1 1 1],...
    'Position',[left bottom (right-left) top-bottom],...
    'String',promptStr);

%---Button panel
 top = 0.95;
 left = 0.80;
 btnWid = 0.15;
 btnHt = 0.065;
 spacing = 0.012;
 frmBorder = 0.02;
 uicontrol(...
    'Parent',fg,...
    'Style','frame',...
    'Position',[left-frmBorder 0.05-frmBorder btnWid+2*frmBorder 0.9+2*frmBorder],...
    'BackgroundColor',[0.50 0.50 0.50]);

%---Response buttons
 Strings = {'Step','Impulse','Bode','Bode Mag.','Nyquist','Nichols','Sigma','Pole-Zero','Root Locus'};
 Tags = {'STEP','IMPULSE','BODE','BODEMAG','NYQUIST','NICHOLS','SIGMA','POLEZERO','RLOCUS'};
 for btnNumber=1:length(Strings)
    uicontrol(...
       'Parent',fg,...
       'Style','pushbutton',...
       'Position',[left top-(btnNumber-1)*(btnHt+spacing)-btnHt btnWid btnHt],...
       'String',Strings{btnNumber},...
       'Callback',@localButtonCB,...
       'UserData',TextField,...
       'Tag',Tags{btnNumber});
 end

%---INFO button
 uicontrol(...
    'Parent',fg,...
    'Style','pushbutton',...
    'Position',[left bottom+btnHt+spacing btnWid btnHt],...
    'String','Info',...
    'Callback',['helpwin ' mfilename]);

%---CLOSE button
 uicontrol(...
    'Parent',fg,...
    'Style','pushbutton',...
    'Position',[left bottom btnWid btnHt],...
    'String','Close',...
    'Callback','close(gcbf)');

%---Show figure
 set(fg,'Visible','on');

%%%%%%%%%%%%%%%%%
% localButtonCB %
%%%%%%%%%%%%%%%%%
function localButtonCB(eventSrc,eventData)
 %---Button callback
  sysstr = str2mat(...
        ' % Generate a random 3rd-order SISO state-space model',...
        ' sys = rss(3);',...
        ' ');
  detailstr = str2mat(' ',' % Click on the line for response details');
  switch get(eventSrc,'Tag')
  case 'STEP'
     cmdStr = str2mat(sysstr,...
        ' % Plot the step response of this system',...
        ' step(sys)',...
        detailstr);
  case 'IMPULSE'
     cmdStr = str2mat(sysstr,...
        ' % Plot the impulse response of this system',...
        ' impulse(sys)',...
        detailstr);
  case 'BODE'
     cmdStr = str2mat(sysstr,...
        ' % Plot the Bode diagram of this system',...
        ' bode(sys)',...
        detailstr);
  case 'BODEMAG'
     cmdStr = str2mat(sysstr,...
        ' % Plot the Bode magnitude diagram of this system',...
        ' bodemag(sys)',...
        detailstr);
  case 'NYQUIST'
     cmdStr = str2mat(sysstr,...
        ' % Plot the Nyquist diagram of this system',...
        ' nyquist(sys)',...
        detailstr);
  case 'NICHOLS'
     cmdStr = str2mat(sysstr,...
        ' % Plot the Nichols chart of this system',...
        ' nichols(sys)',...
        detailstr);
  case 'POLEZERO'
     cmdStr = str2mat(sysstr,...
        ' % Plot the pole-zero map of this system',...
        ' pzmap(sys)',...
        ' ',...
        ' % Move the mouse over the poles/zeros for response details');
  case 'SIGMA'
     cmdStr = str2mat(sysstr,...
        ' % Plot the singular values of this system',...
        ' sigma(sys)',...
        detailstr);
  case 'RLOCUS'
     cmdStr = str2mat(sysstr,...
        ' % Plot the root locus of this system',...
        ' rlocus(sys)',...
        detailstr);
  end
  TextField = get(eventSrc,'UserData');
  set(TextField,'String',cmdStr);
  evalmcw(TextField);
