Request user input
collapse all in page
Syntax
x = input(prompt)
txt = input(prompt,"s")
Description
x = input(prompt)
displaysthe text in prompt
and waits for the user toinput a value and press the Return key. The usercan enter expressions, like pi/4
or rand(3)
,and can use variables in the workspace.
If the user presses the Return keywithout entering anything, then
input
returns anempty matrix.If the user enters an invalid expression at the prompt,then MATLAB® displays the relevant error message, and then redisplaysthe prompt.
txt = input(prompt,"s")
returns the entered text, without evaluating the input as an expression.
Examples
collapse all
Request Numeric Input or Expression
Request a numeric input, and then multiply the input by10.
prompt = "What is the original value? ";x = input(prompt)y = x*10
At the prompt, enter a numeric value or array, such as 42
.
x = 42y = 420
The input
function also accepts expressions. For example, rerun the code.
prompt = "What is the original value? ";x = input(prompt)y = x*10
At the prompt, enter magic(3)
.
x = 8 1 6 3 5 7 4 9 2y = 80 10 60 30 50 70 40 90 20
Request Unprocessed Text Input
Request a simple text response that requiresno evaluation.
prompt = "Do you want more? Y/N [Y]: ";txt = input(prompt,"s");if isempty(txt) txt = 'Y';end
The input
function returns the text exactly as typed. If the input is empty, this code assigns a default value, 'Y'
, to txt
.
Input Arguments
collapse all
prompt
— Text displayed to user
string | character vector
Text displayed to the user, specified as a string or character vector.
To create a prompt that spans several lines, use \n
to indicate each new line. To include a backslash (\
) in the prompt, use \\
.
Output Arguments
collapse all
x
— Result calculated from input
array
Result calculated from input, returned as an array. The typeand dimensions of the array depend upon the response to the prompt.
txt
— Exact text of input
character vector
Exact text of the input, returned as a character vector.
Algorithms
The Workspace browser does not refresh while input
is waiting for a response from the user. Therefore, if you run input
within a script, the Workspace browser does not display changes made to variables in the workspace until the script finishes running.
Version History
Introduced before R2006a
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.
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
- United Kingdom (English)
Contact your local office