Main Content

questdlg

Create question dialog box

Description

Note

In App Designer and apps created with the uifigure function, uiconfirm is recommended over questdlg because it provides additional customization options.

answer = questdlg(quest) creates a modal dialog box that presents a question and returns the user's response -- 'Yes', 'No', 'Cancel', or ''.

By default, the dialog box has three standard buttons, labeled Yes, No, and Cancel.

  • If the user clicks one of these buttons, then the answer value is the same as the label of the pressed button.

  • If the user clicks the close button (X) on the dialog box title bar or presses the Esc key, then the answer value is an empty character vector (' ').

  • If the user presses the Return key, then the answer value is the same as the label of the default button selection. In this case, 'Yes'.

answer = questdlg(quest,dlgtitle) specifies a dialog box title.

answer = questdlg(quest,dlgtitle,defbtn) specifies which button is the default if the user presses the keyboard Return key. The defbtn value must match one of the button labels.

answer = questdlg(quest,dlgtitle,btn1,btn2,defbtn) customizes two of the standard buttons by labeling them with the values of btn1 and btn2. The third standard button is removed. The defbtn value must match the value of btn1 or btn2.

If the user presses the keyboard Return key, and the defbtn value does not match one of the button labels, then the dialog box remains open.

example

answer = questdlg(quest,dlgtitle,btn1,btn2,btn3,defbtn) customizes the third standard button with a label that matches the value of btn3.

answer = questdlg(quest,dlgtitle,opts) specifies an options structure to specify the default button selection and whether to use TeX to interpret the question text.

answer = questdlg(quest,dlgtitle,btn1,btn2,opts) customizes two standard buttons with labels that match the values of btn1 and btn2. The third standard button is removed.

example

answer = questdlg(quest,dlgtitle,btn1,btn2,btn3,opts) customizes the third standard button with a label that matches the value of btn3.

Examples

collapse all

Create a question dialog box with three custom buttons. Assign a different value to the dessert variable depending on the button the user clicks.

answer = questdlg('Would you like a dessert?', ...
	'Dessert Menu', ...
	'Ice cream','Cake','No thank you','No thank you');
% Handle response
switch answer
    case 'Ice cream'
        disp([answer ' coming right up.'])
        dessert = 1;
    case 'Cake'
        disp([answer ' coming right up.'])
        dessert = 2;
    case 'No thank you'
        disp('I''ll bring you your check.')
        dessert = 0;
end

Question dialog box with title "Dessert Menu". The dialog box has a question mark icon with text "Would you like a dessert?" and option buttons labeled "Ice cream", "Cake", and "No thank you". The "No thank you" button is highlighted.

To access the return value assigned to dessert, save the example as a function. For example, create function choosedessert by making this the first line of code.

function dessert = choosedessert

opts.Interpreter = 'tex';
% Include the desired Default answer
opts.Default = 'Don''t know';
% Use the TeX interpreter to format the question
quest = 'Is \Sigma(\alpha - \beta) < 0?';
answer = questdlg(quest,'Boundary Condition',...
                  'Yes','No','Don''t know',opts)

Question dialog box with title "Boundary Condition". The dialog box contains a question mark icon, a formatted equation, and option buttons labeled "Yes", "No", and "Don't know". The "Don't know" button is highlighted.

Input Arguments

collapse all

Dialog box question, specified as a character vector, cell array of character vectors, or string array. The question automatically wraps to fit within the dialog box.

Example: 'What is the velocity?'

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

Example: 'Configuration'

The default button selection, specified as a character vector or string scalar. The default button selection is the value that MATLAB® returns if the user presses the keyboard Return key instead of clicking a button in the dialog box. The default selection must be the same as one of the dialog box button labels. If the defbtn argument value does not match a button label, then the dialog box remains open if the user presses the keyboard Enter key.

Example: 'Cancel'

First customized button label, specified as a character vector or string scalar.

Example: 'Start'

Second customized button label, specified as a character vector or string scalar.

Example: 'Reset'

Third customized button label, specified as a character vector or string scalar.

Example: 'Test'

Dialog box settings specified as a structure. The structure specifies which button is the default button selection, and whether to use TeX to interpret the question text.

The opts structure, must include both of the fields presented in this table. The structure can include additional fields, but questdlg does not use them.

Field

Values

Default

The default button selection, specified as a character vector or string scalar. The default selection must be the same as one of the dialog box button labels.

The default button is the button value that MATLAB returns if the user presses the keyboard Return key instead of clicking a dialog box button.

If the Default field value does not match one of the button labels, then the dialog box does not respond to the user pressing the keyboard Enter key.

Interpreter

'none' (default) or 'tex'. If set to 'tex', then the prompt is rendered using TeX. Button labels and the dialog box title are not affected.

Use TeX markup to add superscripts and subscripts, modify the font type and color, and include special characters in the question 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

©

Example: opts.Default = 'Cancel'

Example: opts.Interpreter = 'tex'

More About

collapse all

Modal Dialog Box

A modal dialog box prevents the user from interacting with other MATLAB windows before responding.

Version History

Introduced before R2006a

See Also

|