Matlab GUIs Making Matlab Interactive. Today’s topics What is a GUI? How does a GUI work? Where do...

34
Matlab GUIs Making Matlab Interactive

Transcript of Matlab GUIs Making Matlab Interactive. Today’s topics What is a GUI? How does a GUI work? Where do...

Page 1: Matlab GUIs Making Matlab Interactive. Today’s topics What is a GUI? How does a GUI work? Where do you begin? Ways to build MATLAB GUIs.

Matlab GUIsMaking Matlab Interactive

Page 2: Matlab GUIs Making Matlab Interactive. Today’s topics What is a GUI? How does a GUI work? Where do you begin? Ways to build MATLAB GUIs.

Today’s topicsWhat is a GUI?

How does a GUI work?

Where do you begin?

Ways to build MATLAB GUIs

Page 3: Matlab GUIs Making Matlab Interactive. Today’s topics What is a GUI? How does a GUI work? Where do you begin? Ways to build MATLAB GUIs.

What is a GUI?Graphical display containing controls

(components) that enable interactive tasks

The user does not need to write a script or type commands to accomplish the tasks

The user need not understand the details of how the tasks are preformed (technically true but dangerous of course)

Page 4: Matlab GUIs Making Matlab Interactive. Today’s topics What is a GUI? How does a GUI work? Where do you begin? Ways to build MATLAB GUIs.

GUI componentsMenus

Toolbars

Push buttons

Radio buttons

List boxes

Sliders

Page 5: Matlab GUIs Making Matlab Interactive. Today’s topics What is a GUI? How does a GUI work? Where do you begin? Ways to build MATLAB GUIs.

How does a GUI work?Most GUIs wait for the user to manipulate a

control and then respond to each action in turn.

Each control has one or more routines known as callbacks

Execution of callbacks is triggered by user action or events

Page 6: Matlab GUIs Making Matlab Interactive. Today’s topics What is a GUI? How does a GUI work? Where do you begin? Ways to build MATLAB GUIs.

Event-driven ProgrammingExample: User clicks a button and callback

execution is asynchronous, or triggered by events external to the software

There are two ways to code callbacksAs MATLAB language functions store in files*As strings containing MATLAB expressions or

commands

*preferred

Page 7: Matlab GUIs Making Matlab Interactive. Today’s topics What is a GUI? How does a GUI work? Where do you begin? Ways to build MATLAB GUIs.

Although you can provide a callback with certain data and make it do anything you want, you cannot control when callbacks will execute.

That is, when your GUI is being used, you have no control over the sequence of events that trigger particular callbacks or what other callbacks might still be running at those times.

This distinguishes event-driven programming from other types of control flow, for example, processing sequential data files.

Page 8: Matlab GUIs Making Matlab Interactive. Today’s topics What is a GUI? How does a GUI work? Where do you begin? Ways to build MATLAB GUIs.

Where do you begin?Like all programs, GUIs begin with design

Who will the user beWhat do you want the GUI to doHow will users interact with the GUIWhat components will be needed for the GUI to

function

Page 9: Matlab GUIs Making Matlab Interactive. Today’s topics What is a GUI? How does a GUI work? Where do you begin? Ways to build MATLAB GUIs.
Page 10: Matlab GUIs Making Matlab Interactive. Today’s topics What is a GUI? How does a GUI work? Where do you begin? Ways to build MATLAB GUIs.

Ways to build GUIsTwo ways:

Use GUIDE (GUI Development Environment), an interactive GUI construction kit

Create code files that generate GUIs as functions or scripts (programmatic approach)

You can create a GUI with GUIDE and then modify it programmatically. However, you cannot create a GUI programmatically and later modify it with GUIDE

Page 11: Matlab GUIs Making Matlab Interactive. Today’s topics What is a GUI? How does a GUI work? Where do you begin? Ways to build MATLAB GUIs.
Page 12: Matlab GUIs Making Matlab Interactive. Today’s topics What is a GUI? How does a GUI work? Where do you begin? Ways to build MATLAB GUIs.

GUIDEOpen GUIDE by typing “guide” at the matlab

prompt

Open GUID by right click on a .fig file and choosing “Open in GUIDE”

We will create a GUI using GUIDE in the lab associated with these lectures

Page 13: Matlab GUIs Making Matlab Interactive. Today’s topics What is a GUI? How does a GUI work? Where do you begin? Ways to build MATLAB GUIs.

ProgrammaticallyLet’s create the simple GUI shown before

Page 14: Matlab GUIs Making Matlab Interactive. Today’s topics What is a GUI? How does a GUI work? Where do you begin? Ways to build MATLAB GUIs.

Functions we will usealign: Align GUI components such as user

interface controls and axes.

axes: Create axes objects.

figure: Create figure objects. A GUI is a figure object.

movegui: Move GUI figure to specified location on screen

uicontrol: Create user interface control objects, such as push buttons, static text, and pop-up menus.

Page 15: Matlab GUIs Making Matlab Interactive. Today’s topics What is a GUI? How does a GUI work? Where do you begin? Ways to build MATLAB GUIs.

1. Create a function file

function simple_gui2

% SIMPLE_GUI2 Select a data set from the pop-up menu, then

% click one of the plot-type push buttons. Clicking the button

% plots the selected data in the axes. [This info shows up using help]

%Must leave a blank space after comment block

end

Page 16: Matlab GUIs Making Matlab Interactive. Today’s topics What is a GUI? How does a GUI work? Where do you begin? Ways to build MATLAB GUIs.

2. Creating the figureA GUI is a figure, so first we create the figure

and position it on the screen

% Initialize and hide the GUI as it is being constructed.

f = figure('Visible','off','Position',[360,500,450,285])

Dist from left, dist from bottom, width, height

Page 17: Matlab GUIs Making Matlab Interactive. Today’s topics What is a GUI? How does a GUI work? Where do you begin? Ways to build MATLAB GUIs.
Page 18: Matlab GUIs Making Matlab Interactive. Today’s topics What is a GUI? How does a GUI work? Where do you begin? Ways to build MATLAB GUIs.

3. Add componentsAdd the three push buttons

% Construct the components.

hsurf = uicontrol('Style','pushbutton',...

'String','Surf','Position',[315,220,70,25]);

hmesh = uicontrol('Style','pushbutton',...

'String','Mesh','Position',[315,180,70,25]);

hcontour = uicontrol('Style','pushbutton',...

'String','Countour','Position',[315,135,70,25]);

Page 19: Matlab GUIs Making Matlab Interactive. Today’s topics What is a GUI? How does a GUI work? Where do you begin? Ways to build MATLAB GUIs.
Page 20: Matlab GUIs Making Matlab Interactive. Today’s topics What is a GUI? How does a GUI work? Where do you begin? Ways to build MATLAB GUIs.

uicontrol function creates the push buttons. Each statement uses a series of property/value pairs to define a push button.

Page 21: Matlab GUIs Making Matlab Interactive. Today’s topics What is a GUI? How does a GUI work? Where do you begin? Ways to build MATLAB GUIs.

Add the pop-up menu and its labels

hpopup = uicontrol('Style','popupmenu',...

'String',{'Peaks','Membrane','Sinc'},...

'Position',[300,50,100,25]);

htext = uicontrol('Style','text','String','Select Data',...

'Position',[325,90,60,15]);

Page 22: Matlab GUIs Making Matlab Interactive. Today’s topics What is a GUI? How does a GUI work? Where do you begin? Ways to build MATLAB GUIs.
Page 23: Matlab GUIs Making Matlab Interactive. Today’s topics What is a GUI? How does a GUI work? Where do you begin? Ways to build MATLAB GUIs.

Add the axes

ha = axes('Units','pixels','Position',[50,60,200,185]);

Align the components (except the axes) along their centers

align([hsurf,hmesh,hcontour,htext,hpopup],'Center','None');

Page 24: Matlab GUIs Making Matlab Interactive. Today’s topics What is a GUI? How does a GUI work? Where do you begin? Ways to build MATLAB GUIs.
Page 25: Matlab GUIs Making Matlab Interactive. Today’s topics What is a GUI? How does a GUI work? Where do you begin? Ways to build MATLAB GUIs.

Make the GUI visible

%Make the GUI visible.

set(f,'Visible','on’)

Try it out>>simple_gui2

Page 26: Matlab GUIs Making Matlab Interactive. Today’s topics What is a GUI? How does a GUI work? Where do you begin? Ways to build MATLAB GUIs.

Initialize the GUINormal the component and figure units so it

resizes properly

% Change units to normalized so components resize automatically.

set([f,hsurf,hmesh,hcontour,htext,hpopup],'Units','normalized');

Page 27: Matlab GUIs Making Matlab Interactive. Today’s topics What is a GUI? How does a GUI work? Where do you begin? Ways to build MATLAB GUIs.

Need to generate the data to plot

% Generate the data to plot.

peaks_data = peaks(35);

membrane_data = membrane;

[x,y] = meshgrid(-8:.5:8);

r = sqrt(x.^2+y.^2) + eps;

sinc_data = sin(r)./r;

Page 28: Matlab GUIs Making Matlab Interactive. Today’s topics What is a GUI? How does a GUI work? Where do you begin? Ways to build MATLAB GUIs.

Create a plot in the axes

% Create a plot in the axes.

current_data = peaks_data;

surf(current_data);

Page 29: Matlab GUIs Making Matlab Interactive. Today’s topics What is a GUI? How does a GUI work? Where do you begin? Ways to build MATLAB GUIs.

% Assign the GUI a name to appear in the window title.

set(f,'Name','Simple GUI')

% Move the GUI to the center of the screen.

movegui(f,'center')

% Make the GUI visible.

set(f,'Visible','on');

Page 30: Matlab GUIs Making Matlab Interactive. Today’s topics What is a GUI? How does a GUI work? Where do you begin? Ways to build MATLAB GUIs.

Programming the GUIProgram the pop-up menu% Read the pop-up menu Value property to

determine which item is currently displayed and make it the current data. This callback automatically has access to current_data because this function is nested at a lower level.

function popup_menu_Callback(source,eventdata)

% Determine the selected data set.

str = get(source, 'String');

val = get(source,'Value');

Page 31: Matlab GUIs Making Matlab Interactive. Today’s topics What is a GUI? How does a GUI work? Where do you begin? Ways to build MATLAB GUIs.

% Set current data to the selected data set.

switch str{val};

case 'Peaks' % User selects Peaks.

current_data = peaks_data;

case 'Membrane' % User selects Membrane.

current_data = membrane_data;

case 'Sinc' % User selects Sinc.

current_data = sinc_data;

end

end

Page 32: Matlab GUIs Making Matlab Interactive. Today’s topics What is a GUI? How does a GUI work? Where do you begin? Ways to build MATLAB GUIs.

Program the push button callbacks

%Push button callbacks. Each callback plots current_data in the

% specified plot type.

function surfbutton_Callback(source,eventdata)

% Display surf plot of the currently selected data.

surf(current_data);

end

Page 33: Matlab GUIs Making Matlab Interactive. Today’s topics What is a GUI? How does a GUI work? Where do you begin? Ways to build MATLAB GUIs.

function meshbutton_Callback(source,eventdata)

% Display mesh plot of the currently selected data.

mesh(current_data);

end

function contourbutton_Callback(source,eventdata)

% Display contour plot of the currently selected data.

contour(current_data);

end

Page 34: Matlab GUIs Making Matlab Interactive. Today’s topics What is a GUI? How does a GUI work? Where do you begin? Ways to build MATLAB GUIs.

Associate callbacks with their components

Revise the uicontrol calls by adding property/value pair

Example:

hsurf = uicontrol('Style','pushbutton','String','Surf',...

'Position',[315,220,70,25],...

'Callback',{@surfbutton_Callback});