function graf2d2(action);
%GRAF2D2 Demonstrates XYZ plots in MATLAB.
%   This window lets you see a variety of surface
%   plots in MATLAB by simply pushing the        
%   buttons on the right side of the window. The 
%   commands that generate the plots are shown   
%   in the MiniCommand Window.                   
%
%   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-2000 The MathWorks, Inc. 
%   $Revision: 5.13 $  $Date: 2000/06/01 03:46:36 $

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

if strcmp(action,'initialize'),
    figNumber=figure( ...
        'Name','Examples of XYZ Plots in MATLAB', ...
        'NumberTitle','off', ...
        'Visible','off');
    % Give the figure a nice black background
    colordef(figNumber,'black')
    axes( ...
        'Units','normalized', ...
        'Position',[0.10 0.45 0.60 0.45]);
  %  clf;
    %===================================
    % 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 some XYZ plots in MATLAB');
    % 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','graf2d2(''eval'')', ...
        'String',promptStr);
    % Save this handle for future use
    set(gcf,'UserData',mcwHndl);

    %====================================
    % 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.03;

    %====================================
    % 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 MESH PLOT button
    btnNumber=1;
    yPos=top-(btnNumber-1)*(btnHt+spacing);
    labelStr='Mesh';
    callbackStr='graf2d2(''grafbutton'')';
    cmdStr=str2mat( ...
        ' % Mesh Plot of Peaks', ...
        ' z=peaks(25);', ...
        ' mesh(z);' ...
    );
    
    % Generic button information
    btnPos=[left yPos-btnHt btnWid btnHt];
    uicontrol( ...
        'Style','pushbutton', ...
        'Units','normalized', ...
        'Position',btnPos, ...
        'String',labelStr, ...
        'Callback',callbackStr, ...
        'UserData',cmdStr);

    %====================================
    % The SURF PLOT button
    btnNumber=2;
    yPos=top-(btnNumber-1)*(btnHt+spacing);
    labelStr='Surf';
    callbackStr='graf2d2(''grafbutton'')';
    cmdStr=str2mat( ...
        ' % Surface Plot of Peaks', ...
        ' z=peaks(25);', ...
        ' surf(z);', ...
        ' colormap(jet);' ...
    );
    
    % Generic button information
    btnPos=[left yPos-btnHt btnWid btnHt];
    uicontrol( ...
        'Style','pushbutton', ...
        'Units','normalized', ...
        'Position',btnPos, ...
        'String',labelStr, ...
        'Callback',callbackStr, ...
        'UserData',cmdStr);

    %====================================
    % The SURFL PLOT button
    btnNumber=3;
    yPos=top-(btnNumber-1)*(btnHt+spacing);
    labelStr='Surfl';
    callbackStr='graf2d2(''grafbutton'')';
    cmdStr=str2mat( ...
        ' % Surface Plot (with Shading) of Peaks', ...
        ' z=peaks(25);', ...
        ' surfl(z);', ...
        ' shading interp;', ...
        ' colormap(pink);' ...
    );
    
    % Generic button information
    btnPos=[left yPos-btnHt btnWid btnHt];
    uicontrol( ...
        'Style','pushbutton', ...
        'Units','normalized', ...
        'Position',btnPos, ...
        'String',labelStr, ...
        'Callback',callbackStr, ...
        'UserData',cmdStr);

    %====================================
    % The CONTOUR GRAPH button
    btnNumber=4;
    yPos=top-(btnNumber-1)*(btnHt+spacing);
    labelStr='Contour';
    callbackStr='graf2d2(''grafbutton'')';
    cmdStr=str2mat( ...
        ' % Contour Plot of Peaks', ...
        ' z=peaks(25);', ...
        ' contour(z,16);' ...
    );
    
    % Generic button information
    btnPos=[left yPos-btnHt btnWid btnHt];
    uicontrol( ...
        'Style','pushbutton', ...
        'Units','normalized', ...
        'Position',btnPos, ...
        'String',labelStr, ...
        'Callback',callbackStr, ...
        'UserData',cmdStr);

    %====================================
    % The QUIVER GRAPH button
    btnNumber=5;
    yPos=top-(btnNumber-1)*(btnHt+spacing);
    labelStr='Quiver';
    callbackStr='graf2d2(''grafbutton'')';
    cmdStr=str2mat( ...
        ' x = -2:.2:2; y = -1:.2:1;', ...
        ' [xx,yy] = meshgrid(x,y);', ...
        ' zz = xx.*exp(-xx.^2-yy.^2);', ...
        ' [px,py] = gradient(zz,.2,.2);', ...
        ' quiver(x,y,px,py,2);' ...
    );
    
    % Generic button information
    btnPos=[left yPos-btnHt btnWid btnHt];
    uicontrol( ...
        'Style','pushbutton', ...
        'Units','normalized', ...
        'Position',btnPos, ...
        'String',labelStr, ...
        'Callback',callbackStr, ...
        'UserData',cmdStr);

    %====================================
    % The SLICE GRAPH button
    btnNumber=6;
    yPos=top-(btnNumber-1)*(btnHt+spacing);
    labelStr='Slice';
    callbackStr='graf2d2(''grafbutton'')';
    cmdStr=str2mat( ...
        ' [x,y,z] = meshgrid(-2:.2:2,-2:.2:2,-2:.2:2);', ...
        ' v = x .* exp(-x.^2 - y.^2 - z.^2);', ...
        ' slice(v,[5 15 21],21,[1 10])', ...
        ' axis([0 21 0 21 0 21]);', ...
        ' colormap(jet)' ...
    );
    
    % Generic 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','graf2d2(''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,'grafbutton'),
    cmdStr=get(gco,'UserData');
    mcwHndl=get(gcf,'UserData');
    set(mcwHndl,'String',cmdStr);
    evalmcw(mcwHndl);

elseif strcmp(action,'eval'),
    cla reset;
    mcwHndl=get(gcf,'UserData');
    evalmcw(mcwHndl);

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

end;    % if strcmp(action, ...
