User`s guide
1 About GUIs in MATLAB
®
Software
How Does a GUI Work?
In the GUI described in “W hat Is a GUI?” on page 1-2, the user selects a data
set from the pop-up menu, then clicks one of the plot type buttons. The mouse
click invokes a function that plots the selected data in the axes.
Most GUIs wait for their user to manipulate a control, and then respond
to each action in turn. Each control, and the G U I itself, has one or more
user-written routines (executable MA TLAB code) known as callbacks,named
for the fact that they “call back” to MATLAB to ask it to do things. The
executionofeachcallbackistriggeredbyaparticularuseractionsuchas
pressing a screen button, clicking a mouse button, selecting a menu item,
typing a string or a numeric value, or passing the cursor over a component.
The GUI then responds to these events. You, as the creator of the GUI, provide
callbacks which define what the components do to handle events.
This kind of programming is often referred to as event-driven programming. In
the example, a button click is one such event. In event-driven programming,
callback execution is asynchronous, that is, it is triggered by ev ents ex ternal to
the software. In the case of MATLAB GUIs, most events are user interactions
with the GUI, but the GUI can re s pond to oth er kinds of events as well, for
example, the creation of a file or connecting a device to the computer.
You can code callbacks in two distinct ways :
• As M ATLAB language f unctions stored in files
• As strings containing MATLAB expressions or commands (such as
'c =
sqrt(a*a + b*b);'
or 'print')
Using functions stored in code files as callbacks is preferable to using strings,
as functions have access to arguments and are more powerful and flexible.
MATLAB scripts (sequences of statements stored in code files that do not
define functions) cannot be used as callbacks.
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,whenyourGUIisbeingused,youhavenocontroloverthesequenceof
events that trigger particular callbacks or what other callbacks might still be
1-4