Skip to content

input

Prompt for user input

input(prompt)
a = input(prompt)

  • When input() is executed, the user is prompted to enter an expression.
  • The input prompt should be a character array, which is the prompt message to be shown to the user.
  • If the output argument a is provided, a stores the value obtained by evaluating the input expression(s) entered by the user.
  • SIMO evaluates the user input expressions as if they were contained in a script.
  • If the output argument a is provided, the last expression entered by the user must return a value and must not run a script.
  • If user enters an empty expression or whitespaces, an empty array is returned.

input(prompt, 's')
a = input(prompt, 's')

  • User input expressions are not evaluated, and are returned as a character array stored in a.
  • The second input argument should always be the character 's'.
  • The input prompt should be a character array, which is the prompt message to be shown to the user.
  • If user enters an empty expression or whitespaces, an empty array is returned.

Example 1: The user is asked to entered a number, which will then be assigned to the variable num and displayed in the console.

num=input('Pls enter a number:');
disp('The number is:')
disp(num)
Pls enter a number:

1;

The number is:

 1.000

Example 2: The user input is returned as the character array fact and is then displayed using disp().

fact=input('Pls tell me a fact:');
disp(['You have told me: ' fact])
Pls tell me a fact:

'The sky is blue';

You have told me: The sky is blue

Example 3: The user is asked to enter a number, and then tell him/her to confirm the input. If the input is 20, then the user is right.

while true
    age=input('Give a number:');
    disp(['You have entered ' num2str(age)]);
    confirm=input('Confirm? [true/false]');
    if confirm
        if age == 20
            disp('Great! You are right')
        else
            disp('Sorry. You are wrong')
        end
        break
    end
end
Give a number:

20;

You have entered 20.00

Confirm? [true/false]

true;

Great! You are right