Creating Graphical User Interfaces | ![]() ![]() |
Modal windows trap all keyboard and mouse events that occur in any visible MATLAB window. This means a modal GUI figure can process the user interactions with any of its components, but does not allow the user to access any other MATLAB window (including the command window). In addition, a modal window remains stacked on top of other MATLAB windows until it is deleted, at which time focus returns to the window that last had focus. See the figure WindowStyle
property for more details.
Use modal figures when you want to force users to respond to your GUI before allowing them to take other actions in MATLAB.
Making a GUI Figure Modal
Set the GUI figure's WindowStyle
property to modal
to make the window modal. You can use the Property Inspector to change this property or add a statement in the initialization section of the application M-file, using the handle returned by openfig
with the set
command.
set(fig,'WindowStyle','modal')
Dismissing a Modal Figure
A GUI using a modal figure must take one of the following actions in a callback routine to release control:
delete(
figure_handle
)
set(
figure_handle
,'Visible','off')
WindowStyle
property to normal
. set(
figure_handle
,'WindowStyle','normal')
The user can also type Control+C in a modal figure to convert it to a normal window.
Obtaining the Figure Handle from Within a Callback. In general, dismissing a modal figure requires the handle of the figure. Since most GUIs hide figure handles to prevent accidental access, the gcbf
(get callback figure) command provides the most effective method to get the figure handle from within a callback routine.
gcbf
returns the handle of the figure containing the object whose callback is executing. This enables you to use gcbf
in the callback of the component that will dismiss the dialog. For example, suppose your dialog includes a push button (tagged pushbutton1
) that closes the dialog. Its callback could include a call to delete
at the end of its callback subfunction.
function varargout = pushbutton1_Callback(h,eventdata,handles,varargin)% Execute code according to dialog design
. . .% Delete figure after user responds to dialog
delete(gcbf)
![]() | Controlling GUI Figure Window Behavior | Application Techniques | ![]() |