input

Interactive input of objects

MuPAD® notebooks will be removed in a future release. Use MATLAB® live scripts instead.

MATLAB live scripts support most MuPAD functionality, though there are some differences. For more information, see Convert MuPAD Notebooks to MATLAB Live Scripts.

Syntax

input(<prompt1>)
input(<prompt1>, x1, <prompt2>, x2, …)

Description

input allows interactive input of MuPAD® objects.

input() displays the prompt “Please enter expression:” and waits for input by the user. The input, terminated by pressing the Return key, is parsed and returned unevaluatedly.

input(prompt1) uses the character string prompt1 instead of the default prompt “Please enter expression:”.

input( prompt1 x1) assigns the input to the identifier or local variable x1. The default prompt is used, if no prompt string is specified.

Several objects can be read with a single input command. Each identifier or variable in the sequence of arguments makes input return a prompt, waiting for input to be assigned to it. A character string preceding an identifier or variable in the argument sequence replaces the default prompt (see Example 2). Arguments that are neither prompt strings nor identifiers or variables are ignored.

The identifiers or variables x1 etc. may have values. These are overwritten by input.

input only parses the input objects for syntactical correctness. It does not evaluate them. Use eval to evaluate the results (see Example 3).

Examples

Example 1

The default prompt is displayed. The input is returned without evaluation:

input()
Please enter expression: << 1 + 2 >> 

A character string is used as a prompt:

input("enter a number: ")
enter a number: << 5
>> 

The input may be assigned to an identifier:

input(x)
Please enter expression: << 5 >> 

x

A user-defined prompt is used, the input is assigned to an identifier:

input("enter a number: ", x)
enter a number: << 6
>> 

x

delete x:

Example 2

If several objects are to be read, for each object a separate prompt can be defined:

input("enter a matrix: ", A, "enter a vector: ", x)
enter a matrix: << matrix([[a11,
a12], [a21, a22]]) >> 
enter
a vector: << matrix([x1, x2]) >> 
matrix([x1, x2])
A, x

delete A, x:

Example 3

The following procedure asks for an expression and a variable. After interactive input, the derivative of the expression with respect to the variable is computed:

interactiveDiff :=
  proc() 
    local f, x;
  begin
     f := input("enter an expression: ");
     x := input("enter an identifier: ");
     print(Unquoted, "The derivative of " . expr2text(f) .
           " with respect to ". expr2text(x) . " is:");
     diff(f, x)
  end_proc:
interactiveDiff()
enter an expression: <<
x^2 + x*y^3 >> 
enter an identifier:
<< x >> 
 The derivative
of x^2 + x*y^3 with respect to x is: 

The function input does not evaluate the input. This leads to the following unexpected result:

f := x^2 + x*y^3:
z := x:
interactiveDiff()
enter
an expression: << f >> 
enter
an identifier: << z >> 
 The
derivative of f with respect to z is: 

The following modification enforces full evaluation via eval:

interactiveDiff :=
  proc() 
    local f, x;
  begin
     f := eval(input("enter an expression: "));
     x := eval(input("enter an identifier: "));
     print(Unquoted, "The derivative of " . expr2text(f) .
           " with respect to ". expr2text(x) . " is:");
     diff(f, x)
  end_proc:
interactiveDiff()
enter an expression: <<
f >> 
enter an identifier:
<< z >> 
 The derivative
of x^2 + x*y^3 with respect to x is: 

delete interactiveDiff, f, z:

Parameters

prompt1, prompt2, …

Input prompts: character strings

x1, x2, …

identifiers or local variables

Return Values

Last input