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



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

function [] = GUI_41()
% Demonstrate how to save and load the state of a GUI system.
% Creates a GUI which allows the user to plot one of four types of
% polynomials.  When the pushbutton is pressed, a new GUI is created which
% displays the coefficients used to create the polynomial, and another
% figure is made which plots the polynomial over a simple range.  The main
% GUI has two menus which allow the user to save the state of the system
% and load the state of the system.
% Suggested exercise:  Modify the code so that the coefficients in the 
% second GUI can be user defined.  You will have to ensure that the
% radiobutton in the first GUI accurately reflects the user's choice, and
% the plot gets updated after the edit.
% Author:  Matt Fig
% Date: 7/30/09

S.X = -10:.01:10;  % The X values for plotting.
S.fh = figure('units','pixels',...
              'position',[200 250 250 200],...
              'closerequestfcn',{@fh_crfcn}); = uibuttongroup('units','pix',...
                     'pos',[20 70 210 90]);
S.rd(1) = uicontrol(,...
                    'position',[20 50 70 30],...
S.SEL = 1;  % The selectedobject property of
S.rd(2) = uicontrol(,...
                    'position',[20 10 70 30],...
S.rd(3) = uicontrol(,...
                    'position',[120 50 70 30],...
S.rd(4) = uicontrol(,...
                    'position',[120 10 70 30],...
S.pb = uicontrol('style','push',...
                 'position',[75 20 100 30],...
                 'string','Plot & Get Coeffs',...
                 'callback',{@pb_call}); = uimenu(S.fh,...
              'label','Save State',...
              'enable','off'); = uimenu(S.fh,...
                 'label','Load State',...

    function [] = pb_call(varargin)
    % Callback for the pushbutton.
        S.RP = round((rand(1,5)*5-rand(1,5)*5)*100)/100;  % Ran polynomial.
        sel = findobj(get(,'selectedobject'));  % See BUG note in GUI_8
        S.SEL = find(S.rd==sel);  % Store current radiobutton.
        set(,{'enable'},{'on';'off'}) % Switch available menu ops.
        switch sel
            case S.rd(1) % Linear
                S.RP(1:3) = 0;
                S.COL = 'r';
            case S.rd(2)  % Quadratic
                S.RP(1:2) = 0;
                S.COL = 'k';
            case S.rd(3)  % Cubic
                S.RP(1) = 0;
                S.COL = 'b';
            case S.rd(4)  % Quartic
                S.COL = 'm';
                % Very unlikely I think.
        S.STR = num2str(S.RP','%3.2f');  % Get a string representation.
        if length(S.fh)==1  % We haven't been here before.
            % This is where we make the other two figures.
            S.fh(2) = figure('name','GUI_41_Plotter',...
   = axes;
            S.fh(3) = figure('units','pixels',...
                             'position',[470 250 250 230],...
            S.tx = uicontrol('units','pixels',...
                             'position',[20 185 210 25],...
                             'string','Ax^4 + Bx^3 + Cx^2 + Dx + E',...
            S.ed(1) = uicontrol('units','pixels',...
                                'position',[50 10 150 25]);
            S.ed(2) = uicontrol('units','pixels',...
                                'position',[50 45 150 25]);
            S.ed(3) = uicontrol('units','pixels',...
                                'position',[50 80 150 25]);
            S.ed(4) = uicontrol('units','pixels',...
                                'position',[50 115 150 25]);
            S.ed(5) = uicontrol('units','pixels',...
                                'position',[50 150 150 25]);  
        % Display polynomial coefficients.
        set(S.ed(1),'string',['E = ',S.STR(5,:)]); 
        set(S.ed(2),'string',['D = ',S.STR(4,:)]);
        set(S.ed(3),'string',['C = ',S.STR(3,:)]);
        set(S.ed(4),'string',['B = ',S.STR(2,:)]);
        set(S.ed(5),'string',['A = ',S.STR(1,:)]);
        S.Y = polyval(S.RP,S.X);  % Use this convenient function.
        figure(S.fh(2))  % Make this figure current for plotting
        plot(S.X,S.Y,S.COL)  % And plot it.

    function [] = fm_call(varargin)
    % Callback for the figure menu.
        switch gcbo
                % Get a name.
                N = inputdlg('Enter Name of Save File','FileName');
                fn = uigetfile('*.mat','Select Saved File');
                    S.fh(2:3) = hgload(fn);
           = get(S.fh(2),'children');
                    ch = get(S.fh(3),'children');
                    S.ed(5:-1:1) = ch(1:5);
                    S.SEL = get(S.fh(2),'userdata');
                    disp('Unable to Load.  Check Name and Try Again.')

    function [] = fh_crfcn(varargin)
    % Closerequestfcn for figures.
       delete(S.fh) % Delete all figures stored in structure. 

Contact us