Create dialog box to gather user input (2024)

Create dialog box to gather user input

collapse all in page

Syntax

answer = inputdlg(prompt)

answer = inputdlg(prompt,dlgtitle)

answer = inputdlg(prompt,dlgtitle,fieldsize)

answer = inputdlg(prompt,dlgtitle,fieldsize,definput)

answer = inputdlg(prompt,dlgtitle,fieldsize,definput,opts)

Description

example

answer = inputdlg(prompt) creates a modal dialog box containing one or more text edit fields and returns the values entered by the user. The return values are elements of a cell array of character vectors. The first element of the cell array corresponds to the response in the edit field at the top of the dialog box. The second element corresponds to the next edit field response, and so on.

example

answer = inputdlg(prompt,dlgtitle) specifies a title for the dialog box.

example

answer = inputdlg(prompt,dlgtitle,fieldsize) specifies the size each edit field.

  • To set a uniform height for all fields, specify fieldsize as a scalar.

  • To set the height and width of each field individually, specify fieldsize as a matrix where each row corresponds to a field.

example

answer = inputdlg(prompt,dlgtitle,fieldsize,definput) specifies the default value for each edit field. The definput input argument must contain the same number of elements as prompt.

example

answer = inputdlg(prompt,dlgtitle,fieldsize,definput,opts)specifies that the dialog box is resizeable in the horizontal direction when opts is set to 'on'. When opts is a structure, it specifies whether the dialog box is resizeable in the horizontal direction, whether it is modal, and whether the prompt text is interpreted.

Examples

collapse all

Dialog Box to Get User Input

Create a dialog box that contains two text edit fields to get integer and colormap name inputs from users.

prompt = {'Enter matrix size:','Enter colormap name:'};dlgtitle = 'Input';fieldsize = [1 45; 1 45];definput = {'20','hsv'};answer = inputdlg(prompt,dlgtitle,fieldsize,definput)

Text Edit Fields of Different Widths

Create an input dialog box titled Customer that contains three edit fields of different widths.

x = inputdlg({'Name','Telephone','Account'},... 'Customer', [1 50; 1 12; 1 7]); 

Create dialog box to gather user input (2)

Interpret prompt Value with TeX Interpreter

Create a dialog box that uses the options structure to render the prompt using TeX markup.

Define the options for the dialog box by specifying:

  • A prompt that includes the \theta character sequence

  • A dialog title

  • A default input value of 30

  • An edit field height equal to 1 row of text and width equal to 40 character units

  • TeX as the interpreter

Then, create the input dialog by specifying these options.

prompt = {'Enter a value of \theta (in degrees)'};dlgtitle = 'Theta Value';fieldsize = [1 40];definput = {'30'};opts.Interpreter = 'tex';answer = inputdlg(prompt,dlgtitle,fieldsize,definput,opts);

Create dialog box to gather user input (3)

Convert Input to Numeric Values

Users can enter scalar or vector values into inputdlg text edit fields. MATLAB® stores the input as a cell array of character vectors. Convert a member of the input cell array to a number, using str2num.

Create an input dialog box that asks users to enter numerical data.

answer = inputdlg('Enter space-separated numbers:',... 'Sample', [1 50])

Create dialog box to gather user input (4)

Suppose that the user enters 1 2 3 4 -5 6+7, and then clicks OK. MATLAB stores the answer as a cell array of character vectors – {'1 2 3 4 -5 6+7'}

Use str2num to convert the cell array to numerals.

user_val = str2num(answer{1})
user_val = 1 2 3 4 -5 13

Input Arguments

collapse all

promptText edit field labels
'Input:' (default) | character vector | cell array of character vectors | string array

Text edit field labels, specified as a character vector, cell array of character vectors, or string array. For cell arrays and string arrays, each element specifies an edit field label. Both types of arrays specify a top to bottom sequence of edit fields in the dialog box.

Example: {'First Name','Last Name'}

dlgtitleDialog box title
'' (default) | character vector | string scalar

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

Example: 'Data'

fieldsizeText edit field height and width
1 (default) | scalar | vector | matrix

Text edit field height and width, specified as a scalar, vector, or matrix.

  • If fieldsize is a scalar, then it specifies the height for all edit fields in lines of text. The width for all edit fields is the maximum that the dialog box allows.

  • If fieldsize is a 1-by-2 vector, all fields in the dialog box have a uniform width and height. The first element specifies the height of each edit field and the second element specifies the width of each edit field. Otherwise, if fieldsize is a column or row vector with number of elements equal to the number of prompts, then each element specifies the height of each corresponding edit field from the top to the bottom of the dialog box. The width for all edit fields is the maximum that the dialog box allows.

  • If fieldsize is an m-by-2 matrix with m equal to the number of prompts, then each row refers to the size of the edit field for the corresponding prompt. The first column specifies the height of the edit field in lines of text. The second column specifies the width of that edit field in character units. The width of a character unit equals the width of the letter x when using the system font.

Note

When the edit field height is 1, users can enter only a single line of text. When the edit field height is any other value, users can enter any number of lines of text.

Example: 2 specifies that the height of each edit field in the dialog box is two lines of text.

Example: [1;2] specifies that the height of the first (top-most) edit field is one line of text and the height of the second edit field is two lines of text.

Example: [1 45] specifies that the height of each edit field is one line of text and the width of each edit field is 45 character units.

Example: [1 50; 2 10] specifies that the first (top-most) edit field is one line high and 50 character units wide and that the second edit field is two lines high and 10 character units wide.

definputDefault input value or values
{' '} (default) | cell array of character vectors | string array

Default input value or values, specified as a cell array of character vectors or string array.

Example: {'Color','1'}

optsDialog box settings
'on' | structure

Dialog box settings specified as 'on' or a structure. When opts is set to 'on', then users can resize the dialog box in the horizontal direction. When opts is a structure, the structure fields specify the options in the following table.

Field

Values

Resize

'off' (default) or 'on'. If set to 'off', then users cannot resize the dialog box. If 'on', then users can resize the window horizontally.

WindowStyle

'modal' (default) or 'normal'. If set to 'modal', then users cannot interact with other windows before responding.

Interpreter

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

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

Example: opts.Resize = 'on'

Example: opts.WindowStyle = 'normal'

Example: opts.Interpreter = 'tex'

Output Arguments

collapse all

answer — Output
empty cell array | cell array of character vectors

Returns a cell array of character vectors containing one input per edit field, starting from the top of the dialog box. Use the str2num function to convert space-delimited and comma-delimited values into row vectors, and semicolon-delimited values into column vectors. For an example, see Convert Input to Numeric Values.

If the user clicks the Cancel button to close the dialog box, then answer is an empty cell array, { }.

If the user presses the keyboard Return key to close the dialog box, then answer is the value of definput. If definput is undefined, then answer is an empty cell array, { }.

More About

collapse all

Modal Dialog Box

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

Tips

  • MATLAB program execution continues even when a modal input dialog box is active. To block program execution until the user responds, use the uiwait function.

Users can enter scalar or vector values into Input dialog boxes. Use str2num to convert space-delimited and comma-delimited values into row vectors and to convert semicolon-delimited values into column vectors. For example, if answer{1} contains '1 2 3;4 -5 6+7i', the conversion produces:

input = str2num(answer{1})input = 1.0000 2.0000 3.0000 4.0000 -5.0000 6.0000 + 7.0000i

Version History

Introduced before R2006a

See Also

listdlg | questdlg

MATLAB Command

You clicked a link that corresponds to this MATLAB command:

 

Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.

Create dialog box to gather user input (5)

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list:

Americas

  • América Latina (Español)
  • Canada (English)
  • United States (English)

Europe

  • Belgium (English)
  • Denmark (English)
  • Deutschland (Deutsch)
  • España (Español)
  • Finland (English)
  • France (Français)
  • Ireland (English)
  • Italia (Italiano)
  • Luxembourg (English)
  • Netherlands (English)
  • Norway (English)
  • Österreich (Deutsch)
  • Portugal (English)
  • Sweden (English)
  • Switzerland
    • Deutsch
    • English
    • Français
  • United Kingdom (English)

Asia Pacific

  • Australia (English)
  • India (English)
  • New Zealand (English)
  • 中国
  • 日本 (日本語)
  • 한국 (한국어)

Contact your local office

Create dialog box to gather user input (2024)
Top Articles
Latest Posts
Article information

Author: Gov. Deandrea McKenzie

Last Updated:

Views: 6360

Rating: 4.6 / 5 (46 voted)

Reviews: 93% of readers found this page helpful

Author information

Name: Gov. Deandrea McKenzie

Birthday: 2001-01-17

Address: Suite 769 2454 Marsha Coves, Debbieton, MS 95002

Phone: +813077629322

Job: Real-Estate Executive

Hobby: Archery, Metal detecting, Kitesurfing, Genealogy, Kitesurfing, Calligraphy, Roller skating

Introduction: My name is Gov. Deandrea McKenzie, I am a spotless, clean, glamorous, sparkling, adventurous, nice, brainy person who loves writing and wants to share my knowledge and understanding with you.