Documentation Center

  • Trial Software
  • Product Updates


Request user input




result = input(prompt) displays the prompt string on the screen, waits for input from the keyboard, evaluates any expressions in the input, and returns the result. To evaluate expressions, the input function can use variables in the current workspace.

  • If you press the Return key without entering anything, then input returns an empty matrix.

  • If you enter an invalid expression at the prompt, then MATLAB® displays the relevant error message, and then redisplays the prompt.


str = input(prompt,'s') returns the entered text as a MATLAB string, without evaluating expressions.


expand all

Numeric or Evaluated Input

Request a numeric input, and then multiply the input by 10.

prompt = 'What is the original value? ';
result = input(prompt)
largernum = result * 10

At the prompt, enter a numeric value or array, such as 42.

result =

largernum =

The input function also accepts expressions. For example, rerun the code.

prompt = 'What is the original value? ';
result = input(prompt)
largernum = result * 10

At the prompt, enter magic(3).

result =
     8     1     6
     3     5     7
     4     9     2

largernum =
    80    10    60
    30    50    70
    40    90    20

The expression does not need to return a numeric result. For example:

prompt = 'What color is the sun? ';
result = input(prompt)

At the prompt, type upper('yellow').

result =

Unprocessed Text Input

Request a simple text response that requires no evaluation.

prompt = 'Do you want more? Y/N [Y]: ';
str = input(prompt,'s');
if isempty(str)
    str = 'Y';

The input function returns the text exactly as typed. If the input is empty, this code assigns a default value, 'Y', to the output string, str.

Input Arguments

expand all

prompt — Query that requests inputstring

Query that requests input, specified as a string.

To create a prompt that spans several lines, use '\n' to indicate each new line. To include a backslash ('\') in the prompt, use '\\'.

Data Types: char

Output Arguments

expand all

result — Result calculated from inputarray

Result calculated from input, returned as an array. The type and dimensions of the array depend upon the response to the prompt.

str — Exact text of inputstring

Exact text of input, returned as a string.

Data Types: char

See Also

| | | |

Was this topic helpful?