Matlab Training Session 13: Creating Guis - PowerPoint PPT Presentation

1 / 26
About This Presentation
Title:

Matlab Training Session 13: Creating Guis

Description:

Underlying the GUI is the backend which controls how the GUI reacts on an event. ... Back End. The code underlying the frontend graphics. ... Backend of Axis and Menu ... – PowerPoint PPT presentation

Number of Views:1226
Avg rating:5.0/5.0
Slides: 27
Provided by: brainb
Category:

less

Transcript and Presenter's Notes

Title: Matlab Training Session 13: Creating Guis


1
Matlab Training Session 13Creating Guis
Course Website http//www.queensu.ca/neurosci/Mat
lab Training Sessions.htm
2
  • Course Outline
  • Term 1
  • Introduction to Matlab and its Interface
  • Fundamentals (Operators)
  • Fundamentals (Flow)
  • Importing Data
  • Functions and M-Files
  • Plotting (2D and 3D)
  • Term 2
  • Term 1 review
  • Plotting (2D and 3D)
  • Statistical Tools in Matlab
  • Nonlinear Curve Fitting
  • Statistical Tools in Matlab II
  • GUIs

3
GUI
  • Graphical User Interface
  • A tool for interacting with the computer


4
How is this useful?
  • Visually interaction with data is often simpler
    and more intuitive
  • Faster manipulation of data
  • Interactive
  • Easy to Learn!
  • Fool Proof


5
Some Examples

6
Components of a GUI
  • Front End
  • point of interaction with user
  • Back End
  • where the action is, the code controlling the
    graphics and the different widgets


7
Components of a GUI
  • Front End
  • Select and place different objects (widgets) on
    the graphical layout. These will the what the
    user interacts with.
  • Underlying the GUI is the backend which controls
    how the GUI reacts on an event.
  • This is called event-driven programming


8
Components of a GUI
  • Back End
  • The code underlying the frontend graphics.
  • Matlab gives a basis and you fill in the details


9
In Matlab
  • Matlab provides interactive tool for creating the
    graphics and underlying code for GUIs
  • Function is called GUIDE
  • Provides some templates for simple GUIs that can
    be expanded as need be
  • Launch GUIDE in Matlab and select on of the
    pre-made templates (ie. not Blank)


10
GUI with Axis and Menu
  • This is the layout for a simple GUI that allows
    you to select one of 5 pre-made plots.
  • Not very useful but a good basis to start with
  • Press the Launch button and save
  • You will see the screen and you can interact with
    it


11
Backend of Axis and Menu
  • When you launch the GUI created the .fig file
    which is the graphics and the .m file which is
    the underlying code that control the widget.
  • Take a Look at the .m file
  • It looks like a mess, but it is not so bad!
  • Lets explore this code
  • First lets understand the basic m-file format
    template for an empty GUI


12
Functions and Callbacks in the M-File
  • Common Input Arguments
  • All functions in the M-file have the following
    input arguments corresponding to the handles
    structure
  • hObject -- the handle to the figure or Callback
    object
  • handles -- structure with handles and user data
  • The handles structure is saved at the end of each
    function with the command
  • guidata(hObject, handles)

13
Functions and Callbacks in the M-File
  • Sharing Data with the Handles Structure
  • When you run a GUI, the M-file creates a
    handles structure that contains all the data for
    GUI objects, such as controls, menus, and axes.
  • The handles structure is passed as an input to
    each callback (user interface).
  • You can use the handles structure to Share data
    between callbacks using the guidata function

14
Functions and Callbacks in the M-File
Sharing Data To store data that is contained in
a variable X, set a field of the handles
structure equal to X and then save the handles
structure with the guidata function
handles.current_data X guidata(hObject,handle
s) You can retrieve the data in any other
callback with the command X
handles.current_data
15
Functions and Callbacks in the M-File
  • You can add code to the following parts of the
    GUI M-file
  • Opening function -- executes before the GUI
    becomes visible to the user.
  • Output function -- outputs data to the command
    line, if necessary.
  • Callbacks -- execute each time the user
    activates the corresponding component of the GUI.

16
Functions and Callbacks in the M-File
  • Opening Function
  • The opening function contains code that is
    executed just before the GUI is made visible to
    the user.
  • You can access all the components for the GUI
    in the opening function, because all objects in
    the GUI are created before the opening function
    is called.
  • You can add code to the opening function to
    perform tasks that need to be done before the
    user has access to the GUI
  • This includes
  • creating data, plots or images, or making the GUI
    blocking with the uiwait command.

17
  • uiwait
  • The command uiwait makes the M-file halt
    execution
  • This is useful to force the program to pause
    execution until a user activates a some component
    in the GUI.
  • The uiresume command allows the M-file to
    resume execution once the user input is complete

18
Functions and Callbacks in the M-File
  • Opening Function
  • For a GUI whose file name is my_gui, the
    definition line for the opening function is
  • function my_gui_OpeningFcn(hObject, eventdata,
    handles, varargin)
  • Besides the arguments hObject and handles, the
    opening function has the following input
    arguments
  • eventdata -- reserved for a future version of
    MATLAB
  • varargin -- command line arguments to untitled

19
Functions and Callbacks in the M-File
  • Opening Function (varargin)
  • All command line arguments are passed to the
    opening function via varargin.
  • If you open the GUI with a property
    name/property value pair as arguments, the GUI
    opens with the property set to the specified
    value.
  • For example
  • my_gui('Position', 71.8 44.9 74.8 19.7)
  • opens the GUI at the specified position, since
    Position is a valid figure property

20
Functions and Callbacks in the M-File
Output Function The output function returns
output arguments to the command line. GUIDE
generates the following output function ---
Outputs from this function are returned to the
command line. function varargout
my_gui_OutputFcn(hObject, eventdata, handles)
Get default command line output from handles
structure varargout1 handles.output
21
Functions and Callbacks in the M-File
  • Callbacks
  • When a user activates a component of the GUI,
    the GUI executes the corresponding callback.
  • The name of the callback is determined by the
    component's Tag property and the type of
    callback.
  • For example, a push button with the Tag
    print_button executes the callback
  • function print_button_Callback(hObject,
    eventdata, handles)

22
Functions and Callbacks in the M-File
  • Output Function
  • By default the output is the handle to the GUI,
    which is assigned to handles.output in the
    opening function.
  • To make the GUI return a different output (eg.
    return the result of a user response, such as
    pressing a push button)
  • Add the command uiwait to the opening function
  • For each component of the GUI where you expect
    a user response, make the callback update the
    value of handles.output, and execute uiresume.
  • handles.output some response string'
  • guidata(hObject, handles)
  • uiresume

23
Property Inspector
  • Adjusts the details of the different widgets


24
Something Useful?
  • Now, lets try and build something that we may
    actually use. How about a simple program that
    combines loading, plotting and calculating some
    simple statistics on preformatted data
  • Create a GUI that will load week8_testdata2.txt,
    plot the data points as well as a linear
    regression
  • Hint, All you need are simple push buttons and an
    axis for plotting


25
Programming GUI controls
  • Push Buttons
  • Work like independent functions
  • Each button function is passed all data from
    the GUI handles
  • --- Executes on button press in Exit.
  • function Exit_Callback(hObject, eventdata,
    handles)
  • hObject handle to Exit (see GCBO)
  • eventdata reserved - to be defined in a future
    version of MATLAB
  • handles structure with handles and user data
    (see GUIDATA)
  • close gcf
  • Detailed descriptions of all GUI callback
    controls can be found within the GUIDE help menu.

26
Layout the GUI
Write a Comment
User Comments (0)
About PowerShow.com