Code covered by the BSD License  

Highlights from
41 Complete GUI Examples

  • GUI_1() Demonstrate how to delete an entry from a uicontrol string.
  • GUI_10() Demonstrate how to make an image visible or invisible by pushbutton.
  • GUI_11()
  • GUI_12() Demonstrate how to control the mouse pointer from a GUI.
  • GUI_13() Demonstrate how to display & change a slider's position with an edit box.
  • GUI_14() Demonstrate colored text in a listbox, & how extract user's choice.
  • GUI_15() Demonstrate an edit text which has copyable but unchangeable text.
  • GUI_16() Demonstrate display & change a slider's position & limits with edit boxes
  • GUI_17() Demonstrate how to have a running clock in a GUI, and timer use.
  • GUI_18() Demonstrate the use of the buttondownfcn for an axes.
  • GUI_19() Demonstrate how to keep track of the number of times an action is taken
  • GUI_2() Demonstrate how to add a new entry to a uicontrol string.
  • GUI_20() Demonstrate how to get the chosen string from a popup.
  • GUI_21() Demonstrate how to get selection from a popup to an edit box & vis versa.
  • GUI_22() Demonstrate how to get selection from a popup to an edit box & vis versa.
  • GUI_23() Demonstrate finding which figure was current before callback execution.
  • GUI_24() Demonstrate how to get data from one GUI to another (data passing).
  • GUI_25() Demonstrate how to make an image loader.
  • GUI_26() Demonstrate how to make the choices in several popups mutually exclusive.
  • GUI_27() Demonstrate how to display the current location of the mouse in an axes.
  • GUI_28() Demonstrate uicontextmenu for an axes click.
  • GUI_29() Demonstrate the use of a uicontrol to manipulate an axes from a GUI,
  • GUI_3() Demonstrate how to hide a uicontrol from the user.
  • GUI_30() Same as GUI_29, except uses callback strings.
  • GUI_31() Demonstrate multiple uicontrol manipulations based on user choices.
  • GUI_32(str) Demonstrate how to get data from a GUI into the base workspace without
  • GUI_33() Demonstrate how to export data with context menu.
  • GUI_34() Demonstrate how to make a simple printscreen GUI. Note that this does
  • GUI_35() Demonstrate how to use toggle buttons to mimic tabbed panels.
  • GUI_36() Demonstrate how to make a custom dialog box which returns information.
  • GUI_37(str,time_out) Demonstrate how to make a password editbox, and nested function
  • GUI_38() Demonstrate bringing the focus to the figure after callback using JAVA.
  • GUI_39() Demonstrate use of nested functions for callbacks and other functions.
  • GUI_4() Demonstrate how to make a multiline editbox.
  • GUI_40() Demonstrate how to use an image as a background of a GUI.
  • GUI_41() Demonstrate how to save and load the state of a GUI system.
  • GUI_5() Demonstrate how to use a pushbutton to delete bits of string and how to
  • GUI_6() Demonstrate how to update one uicontrol with data from others.
  • GUI_7() Demonstrate how to store choice counters for multiple user choices.
  • GUI_8() Demonstrate how to tell which button in a uibuttongroup is selected.
  • GUI_9() Demonstrate one way to let the user know a process is running.
  • previewGUIs() This is a helper GUI which lets the user quickly view all of the example
  • contents.mSyntax Considerations for reading the M-Code:
  • View all files

41 Complete GUI Examples

by

 

27 Jul 2009 (Updated )

41 working GUIs to read and learn.

Editor's Notes:

This file was selected as MATLAB Central Pick of the Week

GUI_32(str)
function [] = GUI_32(str)
% Demonstrate how to get data from a GUI into the base workspace without
% "poofing" unawares.  This is based on GUI_31.  When called with a string
% argument, all of the operations performed by the user will be recorded 
% and returned to the base workspace in a variable of the name given by the
% string.  This happens when the GUI is closed.  If no string argument is
% used, the data is not created in the base workspace.
% 
%  Example:
%          
%           GUI_32('Myops') % Call this then perform a few operations.
%           % After closing the GUI, call:
%           Myops  % at the command line displays a record of operations.
%
% Suggested exercise:  Alter the code so that the variable in the base
% workspace is created upon GUI initialization.  Then update the data after
% every new operation.
%
%
% Author:  Matt Fig
% Date:  7/15/2009

if nargin==0
    str = [];  % User called the GUI without and output name.  
elseif ~ischar(str)
    error('Only a string argument is valid.  See help.')
elseif ~isvarname(str) % User passed something like:  0.?6#
    error('String must be a valid variable name.  See help.')
end

S.STR = str;  % The name of the variable the user wishes to have in base.
S.CNT = 0;  % The number of times user pressed the pushbutton.
S.CHC = [];  % Holds the strings which represent the operations performed.
S.fh = figure('units','pixels',...
              'position',[400 400 300 130],...
              'menubar','none',...
              'name','GUI_32',...
              'numbertitle','off',...
              'resize','off',...
              'deletefcn',{@fig_del,S});
COL = get(S.fh,'color');          
S.pp = uicontrol('style','pop',...
                  'unit','pix',...
                  'position',[10 20 120 30],...
                  'string',{'Add';'Multiply';'Subtract';'Divide';'Power'});
S.ed(1) = uicontrol('style','edit',...
                    'unit','pix',...
                    'position',[10 90 70 30],...
                    'string','3');
S.tx(1) = uicontrol('style','text',...
                    'unit','pix',...
                    'position',[85 90 20 30],...
                    'string','+',...
                    'fontsize',16,...
                    'backgroundcolor',COL);                  
S.ed(2) = uicontrol('style','edit',...
                    'unit','pix',...
                    'position',[110 90 70 30],...
                    'string','2');  
S.tx(2) = uicontrol('style','text',...
                    'unit','pix',...
                    'position',[185 90 20 30],...
                    'string','=',...
                    'fontsize',16,...
                    'backgroundcolor',COL);                 
S.ed(3) = uicontrol('style','edit',...
                    'unit','pix',...
                    'position',[220 90 70 30],...
                    'string','answer');
S.pb = uicontrol('style','push',...
                  'unit','pix',...
                  'position',[160 20 120 30],...
                  'string','Calculate');
set([S.pp,S.pb],'callback',{@pb_call,S});               


function [] = pb_call(varargin)
% Callback for pushbutton
S = varargin{3};  % Get the structure.
N = str2double(get(S.ed(1:2),'string'));  % Numbers from edits to op. on.
VL = get(S.pp,{'str','value'});  % User's choice from popup.

% Now get the string updates and perform operations.
switch VL{1}{VL{2}}  % User's string choice.
    case 'Add'
        A = sum(N);
        str = '+';
    case 'Multiply'
        A = prod(N);
        str = 'x';
    case 'Subtract'
        A = -diff(N);
        str = '-';
    case 'Divide'
        A = N(1)/N(2);
        str = '/';
    case 'Power'
        A = N(1).^N(2);
        str = '^';
    otherwise
end

set(S.tx(1),'string',str)  % Set the operation display.

if varargin{1}==S.pb  % This stuff we only need to do if button is pushed.
    S.CNT = S.CNT + 1;
    set(S.ed(3),'str',A)
    S.CHC{S.CNT,1} = sprintf('%2.2f %s %2.2f %s %2.2f',N(1),str,N(2),'=',A);
    set(S.pb,'callback',{@pb_call,S})
    set(S.fh,'deletefcn',{@fig_del,S})
end


function [] = fig_del(varargin)
S = varargin{3};
if ~isempty(S.STR)
    assignin('base',S.STR,S.CHC) % Assign the data to the base workspace.
end



Contact us