Main Content

uiconfirm

Create confirmation dialog box

Description

uiconfirm(fig,message,title) displays a modal in-app confirmation dialog box in the specified target figure. The target figure must be created with the uifigure function. This syntax displays two options for the user to select, OK and Cancel. You cannot access the figure behind the dialog box while the dialog box is open, but you can access the MATLAB® command prompt.

uiconfirm(fig,message,title,Name,Value) displays the confirmation dialog box with one or more Name,Value arguments that customize the appearance and behavior of the dialog box. For example, you can specify a custom set of options in the dialog box instead of the default, OK and Cancel.

example

selection = uiconfirm(___) returns the user selection as a character vector. Specify the selection output argument with any of the previous syntaxes. When you use this syntax, you cannot access the MATLAB command prompt while the dialog box is open.

example

Examples

collapse all

Create a dialog box that displays the warning icon instead of the default question icon.

fig = uifigure;
selection = uiconfirm(fig, ...
    "Close document?","Confirm Close", ...
    "Icon","warning");

Figure window with a confirmation dialog box. The dialog has a yellow warning icon.

When the user selects an option, uiconfirm returns that choice as a character vector.

Create a confirmation dialog containing three options: Overwrite, Save as new, and Cancel. Specify Save as new as the default option, and specify Cancel as the option that maps to the cancel behavior.

fig = uifigure;
msg = "Saving these changes will overwrite previous changes.";
title = "Confirm Save";
selection = uiconfirm(fig,msg,title, ...
    "Options",["Overwrite","Save as new","Cancel"], ...
    "DefaultOption",2,"CancelOption",3);

Figure window with a confirmation dialog box asking the user to confirm their save. There are three options. The second option, "Save as new", is highlighted in blue.

When the user selects an option, uiconfirm returns their selection as a character vector.

Create a figure with a PolarAxes object and a confirmation dialog box that displays an equation formatted using LaTeX. Include code to process the dialog box selection and plot the equation if the user clicks OK.

fig = uifigure;
ax = polaraxes(fig);
msg = "Do you want to plot $$r = \exp \left(\frac{\theta}{10}\right)?$$";
selection = uiconfirm(fig,msg,"Plot equation","Interpreter","latex");
switch selection
    case 'OK'
        theta = -100:0.1:0;
        r = exp(theta/10);
        polarplot(ax,theta,r);
    case 'Cancel'
        return
end

Figure window with a polar axes and a dialog box asking if you want to plot a polar equation.

Click OK to plot the equation.

Figure window with a spiral curve plotted on a polar axes.

The CloseFcn name-value argument is useful for executing specific tasks when the dialog box closes.

In the MATLAB Editor, create a script that contains the following code. The code creates a figure and defines two callback functions named figcallback and dlgcallback.

  • The figcallback function executes when a user attempts to close the figure window. The function creates a confirmation dialog box in the figure window and specifies the dlgcallback function as the dialog box CloseFcn callback.

  • The dlgcallback function executes when the dialog box closes. The function accesses the SelectedOption field in a struct called event, which MATLAB passes as the second argument to the callback function. If the user selects OK, the function closes the figure window.

fig = uifigure("CloseRequestFcn",@figcallback);

function figcallback(src,event)
    uiconfirm(src,"Close app?","Confirm Close", ...
        "CloseFcn",@dlgcallback);
end

function dlgcallback(src,event)
    if event.SelectedOption == "OK"
        delete(event.Source)
    end
end

Run the script, and then attempt to close the figure window. This creates the confirmation dialog box.

Figure window with a confirmation dialog box that says "Close app?"

For more information about specifying callback functions, see Create Callbacks for Apps Created Programmatically.

Create a confirmation dialog box in App Designer that asks a user to confirm the decision to close the app.

Write a CloseFcn callback for the confirmation dialog box that closes the app figure window if the user selects OK. First, in App Designer Code View, create a private function by selecting Function > Private Function. Then, write the private function so that it matches this code:

function mycallback(app,src,event)
    if event.SelectedOption == "OK"
        delete(app.UIFigure);
    end
end

Finally, to display the dialog box when a user tries to close the app, create a CloseRequestFcn callback for the figure window. Click Callback and select app.UIFigure as the component and CloseRequestFcn as the callback, and then click Add Callback. Replace the body of the callback function that App Designer creates with this code:

uiconfirm(app.UIFigure,'Close document?','Confirm Close', ...
            'CloseFcn',@app.mycallback);

Save and run your app, and then attempt to close the app window to create the confirmation dialog box.

Figure window with a confirmation dialog box that says "Close app?"

For more information about programmatically creating components and specifying callback functions, see Add UI Components to App Designer Programmatically.

Input Arguments

collapse all

Target figure, specified as a Figure object. The figure must be created with the uifigure function.

Message to display, specified as a character vector, cell array of character vectors, or string array. Specify a cell array or string array when your message has multiple lines of text. Each element in the array corresponds to a different line of text.

Dialog box title, specified as a character vector or string scalar.

Name-Value Arguments

Specify optional pairs of arguments as Name1=Value1,...,NameN=ValueN, where Name is the argument name and Value is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

Before R2021a, use commas to separate each name and value, and enclose Name in quotes.

Example: selection = uiconfirm(fig,message,title,'Options',{'Save','Delete','Quit'}) specifies three custom options for the dialog box.

Custom options, specified as a cell array of character vectors or a string array.

Icon, specified as a predefined icon or a custom icon.

Predefined Icon

This table lists the values for the predefined icons. For example, to show the check mark icon, specify the name-value pair 'Icon','success'.

ValueIcon
'question' (default)

Blue circle with a question mark symbol.

'info'

Blue square with the letter "i".

'success'

Green circle with a check mark symbol.

'warning'

Yellow triangle with an exclamation point symbol.

'error'

Red octagon with an exclamation point symbol.

''No icon displays.

Custom Icon

Specify a custom icon as one of these values:

  • A character vector that specifies the file name of an SVG, JPEG, GIF, or PNG image that is on the MATLAB path. Alternatively, you can specify a full path to the image file.

  • A truecolor image array. See Working with Image Types in MATLAB for more information.

Default option, specified as a character vector, string scalar, or a whole number. The default option corresponds to the button in the dialog box that has focus by default.

When you specify a character vector or string scalar, it must match an element in the Options array. However, if you are calling uiconfirm without the Options argument, then DefaultOption must be 'OK' or 'Cancel'.

When you specify a whole number, it must be in the range [1, n], where n is the length of the Options array. If you are calling uiconfirm without the Options argument, then DefaultOption must be 1 or 2.

Cancel option, specified as a character vector, string scalar, or a whole number. The cancel option specifies which option maps to cancel actions in the dialog box.

When you specify a character vector or string scalar, it must match an element in the Options array. However, if you are calling uiconfirm without the Options argument, then CancelOption must be 'OK' or 'Cancel'.

When you specify a whole number, it must be in the range [1, n], where n is the length of the Options array. If you are calling uiconfirm without the Options argument, then CancelOption must be 1 or 2.

Close callback function, specified as one of these values:

  • A function handle.

  • A cell array in which the first element is a function handle. Subsequent elements in the cell array are the arguments to pass to the callback function.

  • A character vector containing a valid MATLAB expression (not recommended). MATLAB evaluates this expression in the base workspace.

This callback is useful for executing specific tasks when the dialog box closes.

When you specify CloseFcn as a function handle (or cell array containing a function handle), MATLAB passes a struct containing event data as an input argument to the callback function. This struct contains the fields described in the following table.

Structure FieldValue
SourceFigure object associated with the dialog box.
EventName'ConfirmDialogClosed'
DialogTitleTitle of the dialog box.
SelectedOptionIndexIndex of the selected option. For n options, the index can be any whole number from 1 to n.
SelectedOptionButton label for the selected option, returned as a character vector.

For more information about specifying callback functions, see Create Callbacks for Apps Created Programmatically.

Dialog text interpreter, specified as:

  • 'none' — Display literal characters.

  • 'tex'— Interpret text using a subset of TeX markup.

  • 'latex'— Interpret text using a subset of LaTeX markup.

  • 'html'— Interpret text using a subset of HTML markup.

TeX Markup

Use TeX markup to add superscripts and subscripts and to include special characters in the text.

Modifiers remain in effect until the end of the text. Superscripts and subscripts are an exception because they modify only the next character or the characters within the curly braces. When you set the interpreter to 'tex', the supported modifiers are as follows.

ModifierDescriptionExample
^{ }Superscript'text^{superscript}'
_{ }Subscript'text_{subscript}'
\bfBold font'\bf text'
\itItalic font'\it text'
\slOblique font (usually the same as italic font)'\sl text'
\rmNormal font'\rm text'
\fontname{specifier}Font name — Replace specifier with the name of a font family. You can use this in combination with other modifiers.'\fontname{Courier} text'
\fontsize{specifier}Font size —Replace specifier with a numeric scalar value in point units.'\fontsize{15} text'
\color{specifier}Font color — Replace specifier with one of these colors: red, green, yellow, magenta, blue, black, white, gray, darkGreen, orange, or lightBlue.'\color{magenta} text'
\color[rgb]{specifier}Custom font color — Replace specifier with a three-element RGB triplet.'\color[rgb]{0,0.5,0.5} text'

This table lists the supported special characters for the 'tex' interpreter.

Character SequenceSymbolCharacter SequenceSymbolCharacter SequenceSymbol

\alpha

α

\upsilon

υ

\sim

~

\angle

\phi

ϕ

\leq

\ast

*

\chi

χ

\infty

\beta

β

\psi

ψ

\clubsuit

\gamma

γ

\omega

ω

\diamondsuit

\delta

δ

\Gamma

Γ

\heartsuit

\epsilon

ϵ

\Delta

Δ

\spadesuit

\zeta

ζ

\Theta

Θ

\leftrightarrow

\eta

η

\Lambda

Λ

\leftarrow

\theta

θ

\Xi

Ξ

\Leftarrow

\vartheta

ϑ

\Pi

Π

\uparrow

\iota

ι

\Sigma

Σ

\rightarrow

\kappa

κ

\Upsilon

ϒ

\Rightarrow

\lambda

λ

\Phi

Φ

\downarrow

\mu

µ

\Psi

Ψ

\circ

º

\nu

ν

\Omega

Ω

\pm

±

\xi

ξ

\forall

\geq

\pi

π

\exists

\propto

\rho

ρ

\ni

\partial

\sigma

σ

\cong

\bullet

\varsigma

ς

\approx

\div

÷

\tau

τ

\Re

\neq

\equiv

\oplus

\aleph

\Im

\cup

\wp

\otimes

\subseteq

\oslash

\cap

\in

\supseteq

\supset

\lceil

\subset

\int

\cdot

·

\o

ο

\rfloor

\neg

¬

\nabla

\lfloor

\times

x

\ldots

...

\perp

\surd

\prime

´

\wedge

\varpi

ϖ

\0

\rceil

\rangle

\mid

|

\vee

\langle

\copyright

©

LaTeX Markup

Use LaTeX markup to format and display mathematical expressions, equations, and special characters. Use dollar symbols around the marked up text. For example, use '$\int_1^{20} x^2 dx$' for inline mode or '$$\int_1^{20} x^2 dx$$' for display mode.

The displayed text uses the default LaTeX font style. You can use LaTeX markup to change the font style.

MATLAB supports most standard LaTeX math mode commands. For more information, see Supported LaTeX Commands.

HTML Markup

Use HTML markup to display links and customize font styles.

The interpreter supports a subset of HTML markup. As a general guideline, the interpreter supports text-related tags and styles. Unsupported tags and styles are ignored.

This table lists the supported elements and element attributes.

HTML ElementAttributesDescription
astyle, target, href, titleHyperlink
abbrstyle, titleAbbreviation or acronym
addressstyleContact information
articlestyleSelf-contained, independent content
asidestyleContent indirectly related to the main content
bstyleBold text
bdistyle, dirContent formatted in a different direction from surrounding text
bdostyle, dirContent formatted in a different direction from surrounding text
bigstyleText one font size level larger than surrounding text (obsolete in HTML5)
blockquotestyle, citeExtended quotation
brn/aLine break
captionstyleCaption or title of a table
centerstyleContent centered horizontally
citestyleTitle of a creative work
codestyleFragment of code
colstyle, align, valign, span, widthColumn within a table
colgroupstyle, align, valign, span, widthGroup of columns within a table
ddstyleTerm or value in a description list
delstyle, datetimeText that was deleted from a document
detailsstyle, openInteractive widget with text visible only when toggled to 'open' state
dlstyleDescription list
dtstyleTerm or value in a description list
emstyleEmphasized text (typically displayed in italic)
fontstyle, color, size, faceText with specified font properties (obsolete in HTML5)
footerstyleFooter
h1. h2, h3, h4, h5, h6styleSection heading — <h1> is the highest level of heading and <h6> is the lowest
headerstyleIntroductory content
hrstyleThematic break
istyleText offset from the surrounding content — by default rendered as italic
insstyle, datetimeText inserted into a document
listyleItem in a list
markstyleMarked or highlighted text
olstyleOrdered list
pstyleParagraph
prestylePreformatted text
sstyleText with a strikethrough
strikestyleText with a strikethrough (obsolete in HTML5)
sectionstyleStandalone section
smallstyleText one font size level smaller than surrounding text (obsolete in HTML5)
substyleSubscript
supstyleSuperscript
strongstyleText with strong importance
tablestyle, width, border, align, valignTable
tbodystyle, align, valignTable body
tdstyle, width, rowspan, colspan, align, valignTable data cell
tfootstyle, align, valignSet of table rows that summarize the table columns
thstyle, width, rowspan, colspan, align, valignTable data cell specified as a header of a group of cells
theadstyle, align, valignSet of table rows that specify the column heads
trstyle, rowspan, align, valignRow of table cells
ttstyleMonospace text (obsolete in HTML5)
ustyleText with an unarticulated annotation — by default rendered as an underline
ulstyleUnordered list

For more information about these elements, see https://developer.mozilla.org/en-US/docs/Web/HTML/Element.

To use HTML markup to create a hyperlink that runs MATLAB code, see Create Hyperlinks that Run Functions.

You can use HTML style attributes to format HTML content. A style attribute is a string of CSS attributes and their values.

These CSS attributes are supported:

  • background-color

  • border-bottom

  • border-bottom-color

  • border-bottom-left-radius

  • border-bottom-right-radius

  • border-bottom-style

  • border-bottom-width

  • border-left

  • border-left-color

  • border-left-style

  • border-left-width

  • border-radius

  • border-right

  • border-right-color

  • border-right-style

  • border-right-width

  • border-spacing

  • border-style

  • border-top

  • border-top-color

  • border-top-left-radius

  • border-top-right-radius

  • border-top-style

  • border-top-width

  • border-width

  • color

  • direction

  • font-family

  • font-size

  • font-style

  • font-weight

  • height

  • hidden

  • line-height

  • margin

  • margin-bottom

  • margin-left

  • margin-right

  • margin-top

  • max-height

  • max-width

  • min-height

  • min-width

  • overflow

  • overflow-wrap

  • overflow-x

  • overflow-y

  • padding

  • padding-bottom

  • padding-left

  • padding-right

  • padding-top

  • text-align

  • text-anchor

  • text-decoration

  • text-indent

  • text-overflow

  • text-shadow

  • text-transform

  • title

  • translate

  • white-space

  • width

For more information about these attributes, see https://developer.mozilla.org/en-US/docs/Web/CSS/Reference.

Version History

Introduced in R2017b

expand all