function hndlaxis(action)
%HNDLAXIS Demonstrates Handle Graphics for axes in MATLAB.
%   This window allows you to assemble a string                      
%   of MATLAB commands that results in a plot in                     
%   the axis in the upper left corner.                               
%                                                                    
%   By playing with the popup menus on the right                     
%   side of the window, you can adjust the scaling                   
%   for the axes, the grid lines, the direction of the               
%   plot and the color of the axes using Handle                      
%   Graphics.                                                        
%                                                                    
%   The MiniCommand Window in the lower                              
%   right shows the list of commands that create                     
%   the plot. If you like, you can even directly edit                
%   the commands in the MiniCommand Window.                          
%   Type control-return to execute code in the                       
%   MiniCommand Window.                                              

%   Ned Gulley, 6-21-93
%   Copyright 1984-2002 The MathWorks, Inc. 
%   $Revision: 5.12 $  $Date: 2002/04/15 03:32:38 $

% Possible actions:
% initialize
% scale
% grid
% direction
% color

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

if strcmp(action,'initialize'),
    oldFigNumber=watchon;

    figNumber=figure( ...
        'Visible','off', ...
        'NumberTitle','off', ...
        'Name','Handle Graphics and the Axis Object');
    axes( ...
        'Units','normalized', ...
        'Position',[0.07 0.45 0.60 0.50]);

    %===================================
    % Set up the MiniCommand Window
    top=0.35;
    left=0.05;
    right=0.70;
    bottom=0.05;
    labelHt=0.05;
    spacing=0.005;
    % 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, ...
        'BackgroundColor',[1 1 1], ...
        'Position',mcwPos, ...
        'Callback','hndlaxis(''quickeval'')', ...
        'String',' % Press buttons at the right for example plots.');
    % Save this handle for future use
    set(gcf,'UserData',mcwHndl);

    %====================================
    % Information for all buttons
    labelColor=[0.8 0.8 0.8];
    top=0.95;
    bottom=0.05;
    left=0.75;
    yInitLabelPos=0.90;
    left=0.75;
    labelWid=0.20;
    labelHt=0.05;
    btnWid=0.20;
    btnHt=0.05;
    % Spacing between the label and the button for the same command
    btnOffset=0.003;
    % Spacing between the button and the next command's label
    spacing=0.05;

    %====================================
    % The CONSOLE frame
    frmBorder=0.02;
    yPos=0.05-frmBorder;
    frmPos=[left-frmBorder yPos btnWid+2*frmBorder 0.9+2*frmBorder];
    h=uicontrol( ...
        'Style','frame', ...
        'Units','normalized', ...
        'Position',frmPos, ...
        'BackgroundColor',[0.50 0.50 0.50]);
    
    %====================================
    % The SCALE TYPE command popup button
    btnNumber=1;
    yLabelPos=top-(btnNumber-1)*(btnHt+labelHt+spacing);
    labelStr=' Scale';
    labelList=' linear| semilogx| semilogy| loglog';
    cmdList=str2mat( ...
        ' ', ...
        ' set(gca,''XScale'',''log'',''YScale'',''linear'');', ...
        ' set(gca,''XScale'',''linear'',''YScale'',''log'');', ...
        ' set(gca,''XScale'',''log'',''YScale'',''log'');');
    callbackStr='hndlaxis eval';
    
    % Generic label information
    labelPos=[left yLabelPos-labelHt labelWid labelHt];
    uicontrol( ...
        'Style','text', ...
        'Units','normalized', ...
        'Position',labelPos, ...
        'BackgroundColor',labelColor, ...
        'HorizontalAlignment','left', ...
        'String',labelStr);

    % Generic popup button information
    btnPos=[left yLabelPos-labelHt-btnHt-btnOffset btnWid btnHt];
    hndl1=uicontrol( ...
        'Style','popup', ...
        'Units','normalized', ...
        'Position',btnPos, ...
        'String',labelList, ...
        'Callback',callbackStr, ...
        'UserData',cmdList);

    %====================================
    % The GRID command popup button
    btnNumber=2;
    yLabelPos=top-(btnNumber-1)*(btnHt+labelHt+spacing);
    labelStr=' Grid';
    labelList=' both| x grid| y grid| neither';
    cmdList=str2mat( ...
        ' set(gca,''XGrid'',''on'',''YGrid'',''on'');', ...
        ' set(gca,''XGrid'',''on'',''YGrid'',''off'');', ...
        ' set(gca,''XGrid'',''off'',''YGrid'',''on'');', ...
        ' set(gca,''XGrid'',''off'',''YGrid'',''off'');');
    callbackStr='hndlaxis eval';
    
    % Generic label information
    labelPos=[left yLabelPos-labelHt labelWid labelHt];
    uicontrol( ...
        'Style','text', ...
        'Units','normalized', ...
        'Position',labelPos, ...
        'BackgroundColor',labelColor, ...
        'HorizontalAlignment','left', ...
        'String',labelStr);

    % Generic popup button information
    btnPos=[left yLabelPos-labelHt-btnHt-btnOffset btnWid btnHt];
    hndl2=uicontrol( ...
        'Style','popup', ...
        'Units','normalized', ...
        'Position',btnPos, ...
        'String',labelList, ...
        'Callback',callbackStr, ...
        'UserData',cmdList);

    %====================================
    % The DIRECTION command popup button
    btnNumber=3;
    yLabelPos=top-(btnNumber-1)*(btnHt+labelHt+spacing);
    labelStr=' Direction';
    labelList=' normal| reverse x| reverse y| rev. both';
    cmdList=str2mat( ...
        ' ', ...
        ' set(gca,''XDir'',''reverse'',''YDir'',''normal'');', ...
        ' set(gca,''XDir'',''normal'',''YDir'',''reverse'');', ...
        ' set(gca,''XDir'',''reverse'',''YDir'',''reverse'');');
    callbackStr='hndlaxis eval';
    
    % Generic label information
    labelPos=[left yLabelPos-labelHt labelWid labelHt];
    uicontrol( ...
        'Style','text', ...
        'Units','normalized', ...
        'Position',labelPos, ...
        'BackgroundColor',labelColor, ...
        'HorizontalAlignment','left', ...
        'String',labelStr);

    % Generic popup button information
    btnPos=[left yLabelPos-labelHt-btnHt-btnOffset btnWid btnHt];
    hndl3=uicontrol( ...
        'Style','popup', ...
        'Units','normalized', ...
        'Position',btnPos, ...
        'String',labelList, ...
        'Callback',callbackStr, ...
        'UserData',cmdList);

    %====================================
    % The COLOR command popup button
    btnNumber=4;
    yLabelPos=top-(btnNumber-1)*(btnHt+labelHt+spacing);
    labelStr=' Color';
    labelList=' default| blue| red| green| cyan ';
    cmdList=str2mat( ...
        ' ', ...
        ' set(gca,''XColor'',''blue'',''YColor'',''blue'');', ...
        ' set(gca,''XColor'',''red'',''YColor'',''red'');', ...
        ' set(gca,''XColor'',''green'',''YColor'',''green'');', ...
        ' set(gca,''XColor'',''cyan'',''YColor'',''cyan'');');
    callbackStr='hndlaxis eval';
    
    % Generic label information
    labelPos=[left yLabelPos-labelHt labelWid labelHt];
    uicontrol( ...
        'Style','text', ...
        'Units','normalized', ...
        'Position',labelPos, ...
        'BackgroundColor',labelColor, ...
        'HorizontalAlignment','left', ...
        'String',labelStr);

    % Generic popup button information
    btnPos=[left yLabelPos-labelHt-btnHt-btnOffset btnWid btnHt];
    hndl4=uicontrol( ...
        'Style','popup', ...
        'Units','normalized', ...
        'Position',btnPos, ...
        'String',labelList, ...
        'Callback',callbackStr, ...
        'UserData',cmdList);

    %====================================
    uicontrol( ...
        'Style','pushbutton', ...
        'Units','normalized', ...
        'Position',[left bottom+2*btnHt+spacing btnWid 2*btnHt], ...
        'String','Info', ...
        'Callback','hndlaxis(''info'')');

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

    % Uncover the figure
    hndlList=[mcwHndl hndl1 hndl2 hndl3 hndl4];
    watchoff(oldFigNumber);
    set(figNumber,'Visible','on', ...
        'UserData',hndlList);
    hndlaxis('eval');

elseif strcmp(action,'info'),
    helpwin(mfilename)

elseif strcmp(action,'quickeval'),
    % Execute the MiniCommand Window text exactly as it is now.
    hndlList=get(gcf,'UserData');
    mcwHndl=hndlList(1);
    evalmcw(mcwHndl);

elseif strcmp(action,'eval'),
    % Assemble and execute the completed command 
    hndlList=get(gcf,'UserData');
    n=length(hndlList);
    mcwHndl=hndlList(1);
    evalStr=str2mat(' x=logspace(-2,0,500);',' plot(x,((sin(1./x)).^2)./x);');
    for count=2:n,
        newStrList=get(hndlList(count),'UserData');
        newStrVal=get(hndlList(count),'Value');
        newStr=deblank(newStrList(newStrVal,:));
        if ~isempty(newStr), evalStr=str2mat(evalStr,newStr); end;
    end;
    set(mcwHndl,'String',evalStr);
    evalmcw(mcwHndl);

end;    % if strcmp(action, ...
