function doc_uipanel1
%% Use Save As in the File menu to create 
% an editable version of this M-file
%
% Create example to illustrate function handle callbacks
% and the use of uipanels

% Copyright 2004 The MathWorks, Inc.

%% Create dummy workspace vars for demo purposes
evalin('base','testVarX = 0:pi/24:2*pi;')
evalin('base','testVarY = cos(0:pi/24:2*pi);')

%% Use system background color for GUI components
panelColor = get(0,'DefaultUicontrolBackgroundColor');

%% ------------ Callback Functions ---------------

% Figure resize function
function figResize(src,evt)
    fpos = get(f,'Position');
    set(botPanel,'Position',...
        [1/20 1/20 fpos(3)-.1 fpos(4)*8/35])
    set(rightPanel,'Position',...
        [fpos(3)*85/120 fpos(4)*8/35 fpos(3)*35/120 fpos(4)*27/35])
    set(centerPanel,'Position',...
        [1/20 fpos(4)*8/35 fpos(3)*85/120 fpos(4)*27/35]);
end
    
% Bottom panel resize function
function botPanelResize(src, evt)
    bpos = get(botPanel,'Position');
    set(plotButton,'Position',...
        [bpos(3)*10/120 bpos(4)*2/8 bpos(3)*24/120 2])
    set(holdToggle,'Position',...
        [bpos(3)*45/120 bpos(4)*2/8 bpos(3)*24/120 2])
    set(popUp,'Position',...
        [bpos(3)*80/120 bpos(4)*2/8 bpos(3)*24/120 2])
    set(popUpLabel,'Position',...
        [bpos(3)*80/120 bpos(4)*4/8 bpos(3)*24/120 2])
end

% Right panel resize function
function rightPanelResize(src,evt)
    rpos = get(rightPanel,'Position');
    set(listBox,'Position',...
        [rpos(3)*4/32 rpos(4)*2/27 rpos(3)*24/32 rpos(4)*20/27]);
    set(listBoxLabel,'Position',...
        [rpos(3)*4/32 rpos(4)*24/27 rpos(3)*24/32 rpos(4)*2/27]);
end

%% Callback for list box
  function listBoxCallback(src,evt)
        % Load workspace vars into list box
        vars = evalin('base','who');
        set(src,'String',vars)
    end % listBoxCallback

%% Callback for plot button
    function plotButtonCallback(src,evt)
        % Get workspace variables
        vars = get(listBox,'String');
        var_index = get(listBox,'Value');
        if length(var_index) ~= 2
            errordlg('You must select two variables',...
            'Incorrect Selection','modal')
        return
        end
        % Get data from base workspace
        x = evalin('base',vars{var_index(1)});
        y = evalin('base',vars{var_index(2)});
        % Get plotting command
        selected_cmd = get(popUp,'Value');
        % Make the GUI axes current and create plot
        axes(a)
        switch selected_cmd
        case 1 % user selected plot
            plot(x,y)
        case 2 % user selected bar
            bar(x,y)
        case 3 % user selected stem
            stem(x,y)
        end
    end % plotButtonCallback
    
%% Callback for hold state toggle button
    function holdToggleCallback(src,evt)
        button_state = get(src,'Value');
        if button_state == get(src,'Max')
            % toggle button is depressed
            hold(a,'on')
            set(src,'String','Hold On')
        elseif button_state == get(src,'Min')
            % toggle button is not depressed
            hold(a,'off')
            set(src,'String','Hold Off')
        end
    end % holdToggleCallback

%% ------------ GUI layout ---------------

%% Set up the figure and defaults
f = figure('Units','characters',...
        'Position',[30 30 120 35],...
        'Color',panelColor,...
        'HandleVisibility','callback',...
        'IntegerHandle','off',...
        'Renderer','painters',...
        'Toolbar','figure',...
        'NumberTitle','off',...
        'Name','Workspace Plotter',...
        'ResizeFcn',@figResize);

%% Create the bottom uipanel
botPanel = uipanel('BorderType','etchedin',...
    'BackgroundColor',panelColor,...
    'Units','characters',...
    'Position',[1/20 1/20 119.9 8],...
    'Parent',f,...
    'ResizeFcn',@botPanelResize);

%% Create the right side panel
rightPanel = uipanel('bordertype','etchedin',...
    'BackgroundColor',panelColor,...
    'Units','characters',...
    'Position',[88 8 32 27],...
    'Parent',f,...
    'ResizeFcn',@rightPanelResize);

%% Create the center panel
centerPanel = uipanel('bordertype','etchedin',...
    'Units','characters',...
    'Position', [1/20 8 88 27],...
    'Parent',f);

%% Add an axes to the center panel
a = axes('parent',centerPanel);

%% Add listbox and label
listBoxLabel = uicontrol(f,'Style','text','Units','characters',...
        'Position',[4 24 24 2],...
        'String','Select 2 Workspace Variables',...
        'BackgroundColor',panelColor,...
        'Parent',rightPanel);
listBox = uicontrol(f,'Style','listbox','Units','characters',...
        'Position',[4 2 24 20],...
        'BackgroundColor','white',...
        'Max',10,'Min',1,...
        'Parent',rightPanel,...
        'Callback',@listBoxCallback);

%% Add popup and label
popUpLabel = uicontrol(f,'Style','text','Units','characters',...
        'Position',[80 4 24 2],...
        'String','Plot Type',...
        'BackgroundColor',panelColor,...
        'Parent',botPanel);
popUp = uicontrol(f,'Style','popupmenu','Units','characters',...
        'Position',[80 2 24 2],...
        'BackgroundColor','white',...
        'String',{'Plot','Bar','Stem'},...
        'Parent',botPanel);
    
%% Add hold and plot buttons      
holdToggle = uicontrol(f,'Style','toggle','Units','characters',...
        'Position',[45 2 24 2],...
        'String','Hold State',...
        'Parent',botPanel,...
        'Callback',@holdToggleCallback);
plotButton = uicontrol(f,'Style','pushbutton','Units','characters',...
        'Position',[10 2 24 2],...
        'String','Create Plot',...
        'Parent',botPanel,...
        'Callback',@plotButtonCallback);
    
%% Initialize list box and make sure
% the hold toggle is set correctly
    listBoxCallback(listBox,[])
    holdToggleCallback(holdToggle,[])  
end % uipanel1




