function xpctrl(action)
%XPCTRL	Demonstrates basic control commands in the Control Toolbox.

%	Ned Gulley, 6-21-93
%	Copyright (c) 1986-1999 The Mathworks, Inc. All Rights Reserved.
%	$Revision: 1.6 $ 

if nargin<1,
    action='initialize';
end;

if strcmp(action,'initialize'),
    figNumber=figure( ...
	'Name','Examples of Basic Control Commands', ...
        'NumberTitle','off', ...
	'Visible','off');

    figDefaultAxesPos=[0.10 0.45 0.65 0.45];
    set(figNumber,'DefaultAxesPosition',figDefaultAxesPos);

    axes( ...
	'Units','normalized', ...
        'Position',figDefaultAxesPos);

    %===================================
    % Set up the MiniCommand Window
    top=0.35;
    left=0.05;
    right=0.75;
    bottom=0.05;
    labelHt=0.05;
    spacing=0.005;
    promptStr=str2mat(' ',...
              ' % Press the buttons at the right to see examples of basic', ...
	      ' % control commands.');
    % First, the MiniCommand Window frame
    frmBorder=0.02;
    frmPos=[left-frmBorder bottom-frmBorder ...
	(right-left)+2*frmBorder (top-bottom)+2*frmBorder];
    uicontrol( ...
        'Style','frame', ...
        'Units','normalized', ...
        'Position',frmPos, ...
	'BackgroundColor',[0.50 0.50 0.50]);
    % Then the text label
    labelPos=[left top-labelHt (right-left) labelHt];
    uicontrol( ...
	'Style','text', ...
        'Units','normalized', ...
        'Position',labelPos, ...
        'BackgroundColor',[0.50 0.50 0.50], ...
	'ForegroundColor',[1 1 1], ...
        'String','MiniCommand Window');
    % Then the editable text field
    mcwPos=[left bottom (right-left) top-bottom-labelHt-spacing];
    mcwHndl=uicontrol( ...
	'Style','edit', ...
	'HorizontalAlignment','left', ...
        'Units','normalized', ...
        'Max',10, ...
	'Tag','mcw', ...
        'BackgroundColor',[1 1 1], ...
        'Position',mcwPos, ...
	'Callback','xpctrl(''eval'')', ...
        'String',promptStr);

    %====================================
    % Information for all buttons
    labelColor=[0.8 0.8 0.8];
    top=0.95;
    left=0.80;
    btnWid=0.15;
    btnHt=0.08;
    % Spacing between the button and the next command's label
    spacing=0.04;

    %====================================
    % The CONSOLE frame
    frmBorder=0.02;
    yPos=0.05-frmBorder;
    frmPos=[left-frmBorder yPos btnWid+2*frmBorder 0.9+2*frmBorder];
    uicontrol( ...
        'Style','frame', ...
        'Units','normalized', ...
        'Position',frmPos, ...
	'BackgroundColor',[0.50 0.50 0.50]);
    
    %====================================
    % The ROOT LOCUS button
    btnNumber=1;
    yPos=top-(btnNumber-1)*(btnHt+spacing);
    labelStr='Root Locus';
    callbackStr='xpctrl(''demobutton'')';
    cmdStr=str2mat( ...
        ' sys=rss(3);', ...   
        ' rlocus(sys)', ...
        ' title(''Root Locus of a Random System'');' ...
    );        
    
    % Generic popup button information
    btnPos=[left yPos-btnHt btnWid btnHt];
    uicontrol( ...
	'Style','pushbutton', ...
        'Units','normalized', ...
        'Position',btnPos, ...
        'String',labelStr, ...
        'Callback',callbackStr, ...
	'UserData',cmdStr);

    %====================================
    % The BODE button
    btnNumber=2;
    yPos=top-(btnNumber-1)*(btnHt+spacing);
    labelStr='Bode';
    callbackStr='xpctrl(''demobutton'')';
    cmdStr=str2mat( ...
        ' sys=rss(3);', ...   
        ' [mag,phase,w]=bode(sys);', ...
	' loglog(w,mag(:))', ...
        ' title(''Bode Magnitude Plot of a Random System'');' ...
    );
    
    % Generic popup button information
    btnPos=[left yPos-btnHt btnWid btnHt];
    uicontrol( ...
	'Style','pushbutton', ...
        'Units','normalized', ...
        'Position',btnPos, ...
        'String',labelStr, ...
        'Callback',callbackStr, ...
	'UserData',cmdStr);

    %====================================
    % The STEP RESPONSE button
    btnNumber=3;
    yPos=top-(btnNumber-1)*(btnHt+spacing);
    labelStr='Step';
    callbackStr='xpctrl(''demobutton'')';
    cmdStr=str2mat( ...
        ' sys=rss(3);', ...   
        ' [y,t]=step(sys);', ...
	' plot(t,y)', ...
        ' title(''Step Response Plot of a Random System'');', ...
	' xlabel(''Time'');' ...
    );        
    
    % Generic popup button information
    btnPos=[left yPos-btnHt btnWid btnHt];
    uicontrol( ...
	'Style','pushbutton', ...
        'Units','normalized', ...
        'Position',btnPos, ...
        'String',labelStr, ...
        'Callback',callbackStr, ...
	'UserData',cmdStr);

    %====================================
    % The IMPULSE button
    btnNumber=4;
    yPos=top-(btnNumber-1)*(btnHt+spacing);
    labelStr='Impulse';
    callbackStr='xpctrl(''demobutton'')';
    cmdStr=str2mat( ...
        ' sys=rss(3);', ...   
        ' [y,t]=impulse(sys);', ...
	' plot(t,y)', ...
        ' title(''Impulse Response Plot of a Random System'');', ...
	' xlabel(''Time'');' ...
    );        
    
    % Generic popup button information
    btnPos=[left yPos-btnHt btnWid btnHt];
    uicontrol( ...
 	'Style','pushbutton', ...
        'Units','normalized', ...
        'Position',btnPos, ...
        'String',labelStr, ...
        'Callback',callbackStr, ...
	'UserData',cmdStr);

    %====================================
    % The NYQUIST button
    btnNumber=5;
    yPos=top-(btnNumber-1)*(btnHt+spacing);
    labelStr='Nyquist';
    callbackStr='xpctrl(''demobutton'')';
    cmdStr=str2mat( ...
        ' sys=rss(3);', ...   
        ' [re,im,w]=nyquist(sys);', ...
	' plot(re(:),im(:),re(:),-im(:))', ...
        ' title(''Nyquist Plot of a Random System'');', ...
	' xlabel(''Real'');', ...
	' ylabel(''Imaginary'');' ...
    );        
    
    % Generic popup button information
    btnPos=[left yPos-btnHt btnWid btnHt];
    uicontrol( ...
	'Style','pushbutton', ...
        'Units','normalized', ...
        'Position',btnPos, ...
        'String',labelStr, ...
        'Callback',callbackStr, ...
	'UserData',cmdStr);

    %====================================
    % The INFO button
    uicontrol( ...
	'Style','push', ...
        'Units','normalized', ...
        'Position',[left bottom+btnHt+spacing btnWid btnHt], ...
        'String','Info', ...
        'Callback','xpctrl(''info'')');

    %====================================
    % The CLOSE button
    uicontrol( ...
	'Style','push', ...
        'Units','normalized', ...
        'Position',[left bottom btnWid btnHt], ...
        'String','Close', ...
        'Callback','close(gcf)');

    % Now uncover the figure
    set(figNumber,'Visible','on');

elseif strcmp(action,'demobutton'),
    cmdStr=get(gco,'UserData');
    mcwHndl=findobj(gcf,'Tag','mcw');
    set(mcwHndl,'String',cmdStr);
    evalmcw(mcwHndl);

elseif strcmp(action,'eval'),
    mcwHndl=findobj(gcf,'Tag','mcw');
    cmdStr=get(mcwHndl,'String');
    evalmcw(mcwHndl);

elseif strcmp(action,'info'),
    ttlStr='Basic Control Commands';
    hlpStr= {'                                               ';  
         ' This window lets you see a variety of Control ';  
         ' Toolbox commands by simply pressing the       ';  
         ' buttons on the right side of the window. The  ';  
         ' commands that generate the plots are shown    ';  
         ' in the MiniCommand Window.                    ';  
         '                                               ';  
         ' The models generated are random with the      ';  
         ' constraint that they are guaranteed to be     ';  
         ' stable.                                       ';  
         '                                               ';  
         ' If you like, you can even directly edit the   ';  
         ' commands in the MiniCommand Window.           ';  
         ' Type control-return to execute code in the    ';  
         ' MiniCommand Window.                           ';  
         '                                               ';  
         ' File name: xpctrl.m                           '};
    helpwin(hlpStr,ttlStr);                

end;    % if strcmp(action, ...
