This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materials including this page, select Japan from the country navigator on the bottom of this page.


Create dialog box that gathers user input


answer = inputdlg(prompt)
answer = inputdlg(prompt,dlg_title)
answer = inputdlg(prompt,dlg_title,num_lines)
answer = inputdlg(prompt,dlg_title,num_lines,defAns)
answer = inputdlg(prompt,dlg_title,num_lines,defAns,options)


answer = inputdlg(prompt) creates a modal dialog box and returns user input for multiple prompts. prompt is a cell array containing the text prompt.


A modal dialog box prevents the user from interacting with other windows before responding. For more information, see WindowStyle in Figure Properties.

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

answer = inputdlg(prompt,dlg_title,num_lines) num_lines specifies the number of lines for each user-entered value. num_lines can be a scalar, column vector, or a m x 2 array.

  • If num_lines is a scalar, it applies to all prompts.

  • If num_lines is a column vector, each element specifies the number of lines of input for a prompt.

  • If num_lines is an array, it must be size m-by-2, where m is the number of prompts on the dialog box. Each row refers to a prompt. The first column specifies the number of lines of input for a prompt. The second column specifies the width of the field in characters.

answer = inputdlg(prompt,dlg_title,num_lines,defAns) defAns specifies the default value to display for each prompt. defAns must contain the same number of elements as prompt and all elements must be of type char.

answer = inputdlg(prompt,dlg_title,num_lines,defAns,options) If options is 'on', then the dialog is resizable in the horizontal direction. If options is a structure, the fields shown in the following table are recognized:




Can be 'on' or 'off' (default). If 'on', the window is resizable horizontally.


Can be either 'normal' or 'modal' (default).


Can be either 'none' (default) or 'tex'. If the value is 'tex', the prompt is rendered using TeX.

If the user clicks the Cancel button to close an inputdlg box, the dialog returns an empty cell array:

answer = 


Example 1

Create a dialog box to input an integer and colormap name. Allow one line for each value.

prompt = {'Enter matrix size:','Enter colormap name:'};
dlg_title = 'Input';
num_lines = 1;
defaultans = {'20','hsv'};
answer = inputdlg(prompt,dlg_title,num_lines,defaultans);

Example 2

Create a dialog box named to accept space-separated numbers. MATLAB® stores the input as characters, so convert the characters to numbers using str2num.

x = inputdlg('Enter space-separated numbers:',...
             'Sample', [1 50]);
data = str2num(x{:}); 

Example 3

Create a dialog box to display input fields of different widths.

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

Example 4

Create a dialog box, specifying a value in the options structure. In this case, the options structure specifies TeX to be the interpreter.

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


inputdlg uses the uiwait function to suspend execution until the user responds.

The returned variable answer is a cell array containing one answer per text entry field, starting from the top of the dialog box.

To convert a member of the cell array to a number, use str2num. To do this, you can add the following code to the end of any of the examples below:

% Use curly bracket for subscript
[val status] = str2num(answer{1});  
if ~status
    % Handle empty value returned 
    % for unsuccessful conversion
    % ...
% val is a scalar or matrix converted from the first input
Users can enter scalar or vector values into inputdlg fields. Use the str2num function to convert space- and comma-delimited values into row vectors, and semicolon-delimited values into column vectors. For example, if answer{1} contains '1 2 3;4 -5 6+7i', the conversion produces:
val = str2num(answer{1})
val =
   1.0000     2.0000     3.0000
   4.0000    -5.0000     6.0000 + 7.0000i

Introduced before R2006a

Was this topic helpful?