Create figure for designing apps
fig = uifigure;
Create a UI figure with a specific title.
fig = uifigure('Name','Plotted Results');
Position property value.
p = fig.Position
ans = 680 678 560 420
Create two UI figure windows. Block interactions in Figure 1 by specifying
'modal' as the
WindowStyle property value for Figure 2. Notice that you cannot interact with Figure 1 until Figure 2 is closed.
fig1 = uifigure('Name','Figure 1'); fig1.Position = [500 500 370 270]; fig2 = uifigure('Name','Figure 2'); fig2.Position = [540 450 370 270]; fig2.WindowStyle = 'modal';
CloseRequestFcn callback to
open a modal Confirmation dialog box when the user tries to close the
Copy and paste this code into the MATLAB® Editor, and then run
function closeFig fig = uifigure('Position',[100 100 425 275]); fig.CloseRequestFcn = @(fig,event)my_closereq(fig); function my_closereq(fig,selection) selection = uiconfirm(fig,'Close the figure window?',... 'Confirmation'); switch selection case 'OK' delete(fig) case 'Cancel' return end end end
Click the figure close button. The Confirmation dialog box opens.
Change the mouse pointer symbol that displays when you hover over a push button.
This program file, called
setMousePointer.m, shows you how to:
Create a UI figure which executes custom code when the mouse is moved over a button. To do this, use the
@ operator to assign the
mouseMoved function handle to the
WindowButtonMotionFcn property of the figure.
Create a push button and specify its coordinates and label.
Create a callback function called
mouseMoved with the custom code you want to execute when the mouse moves over the button. In the function, query the
CurrentPoint property to determine the mouse pointer coordinates. Set the
Pointer property to
'hand' if the pointer coordinates are within the push button coordinates.
setMousePointer. Then move the mouse over the push button to see the mouse pointer symbol change.
function setMousePointer fig = uifigure('Position',[500 500 375 275]); fig.WindowButtonMotionFcn = @mouseMoved; btn = uibutton(fig); btnX = 50; btnY = 50; btnWidth = 100; btnHeight = 22; btn.Position = [btnX btnY btnWidth btnHeight]; btn.Text = 'Submit Changes'; function mouseMoved(src,event) mousePos = fig.CurrentPoint; if (mousePos(1) >= btnX) && (mousePos(1) <= btnX + btnWidth) ... && (mousePos(2) >= btnY) && (mousePos(2) <= btnY + btnHeight) fig.Pointer = 'hand'; else fig.Pointer = 'arrow'; end end end
comma-separated pairs of
the argument name and
Value is the corresponding value.
Name must appear inside quotes. You can specify several name and value
pair arguments in any order as
My Appas the title of the UI figure.
The properties listed here are a subset of the available properties. For the full list, see UI Figure Properties.
'WindowStyle'— Window style
Window style, specified as one of the following:
'normal' — The figure window is independent of other windows, and the other windows are accessible while the figure is displaying.
'modal' — The figure displays on top of all existing figure windows, making them inaccessible as long as the top figure exists and remains modal. However, any new figures created after a modal figure will display.
When multiple modal windows exist, the most recently created window keeps focus and stays above all other windows until it becomes invisible, or is returned to a normal window style, or is deleted. At that time, focus reverts to the window that last had focus.
These are some important characteristics of the
WindowStyle property and some recommended best practices:
When you create UI windows, always specify the
WindowStyle property. If you also want to set
properties of the figure, then set the
You can change the
WindowStyle property of a figure at any time, including when the figure is visible and contains children. However on some systems, setting this property might cause the figure to flash or disappear and reappear, depending on the system's implementation of normal and modal windows. For best visual results, set the
WindowStyle property at creation time or when the figure is invisible.
WindowStyle is set to
'modal', the UI figure window blocks keyboard and mouse interactions in a UI figure window that was created before it and has its
Visible property set to
'on'. For instance, in this example Figure 3 is modal with respect to Figure 2 and Figure 2 is modal with respect to Figure 1.
fig1 = uifigure('Name','Figure 1'); fig1.WindowStyle = 'modal'; fig2 = uifigure('Name','Figure 2'); fig2.WindowStyle = 'modal'; fig3 = uifigure('Name','Figure 3'); fig3.WindowStyle = 'modal';
Unlike modal figures created with the
figure function, modal figures created with the
uifigure function do not block access to figures created with the figure function or the MATLAB desktop. Interactions with application windows other than MATLAB are also not blocked.
Typing Ctrl+C when a modal figure has focus causes that figure to revert to a
WindowStyle property setting. This allows the user to type at the command line.
UI figures with the
WindowStyle property set to
'modal' and the
Visible property set to
'off' do not behave modally until MATLAB makes them visible. Therefore, you can hide a modal window for later reuse, instead of destroying it.
Modal figures do not display menu children, built-in menus, or toolbars. But, it is not an error to create menus in a modal figure or to change the
WindowStyle property setting to
'modal' on a figure with menu children. The
Menu objects exist and the figure retains them. If you reset the UI figure
WindowStyle property to
'normal', the menus display.
Use the graphics root object to set default values on the root level for
other types of objects. For example, set the default colormap for all future
figures to the