Octave has three functions that make it easy to prompt users for input. The input
and menu
functions are normally used for managing an interactive dialog with a user, and the keyboard
function is normally used for doing simple debugging.
Print prompt and wait for user input.
For example,
input ("Pick a number, any number! ")
prints the prompt
Pick a number, any number!
and waits for the user to enter a value. The string entered by the user is evaluated as an expression, so it may be a literal constant, a variable name, or any other valid Octave code.
The number of return arguments, their size, and their class depend on the expression entered.
If you are only interested in getting a literal string value, you can call input
with the character string "s"
as the second argument. This tells Octave to return the string entered by the user directly, without evaluating it first.
Because there may be output waiting to be displayed by the pager, it is a good idea to always call fflush (stdout)
before calling input
. This will ensure that all pending output is written to the screen before your prompt.
Ask the user a yes-or-no question.
Return logical true if the answer is yes or false if the answer is no.
Takes one argument, prompt, which is the string to display when asking the question. prompt should end in a space; yes-or-no
adds the string ‘(yes or no) ’ to it. The user must confirm the answer with RET and can edit it until it has been confirmed.
See also: input.
For input
, the normal command line history and editing functions are available at the prompt.
Octave also has a function that makes it possible to get a single character from the keyboard without requiring the user to type a carriage return.
Read a single keystroke from the keyboard.
If called with an argument, don’t wait for a keypress.
For example,
x = kbhit ();
will set x to the next character typed at the keyboard as soon as it is typed.
x = kbhit (1);
is identical to the above example, but doesn’t wait for a keypress, returning the empty string if no key is available.
© 1996–2018 John W. Eaton
Permission is granted to make and distribute verbatim copies of this manual provided the copyright notice and this permission notice are preserved on all copies.
Permission is granted to copy and distribute modified versions of this manual under the conditions for verbatim copying, provided that the entire resulting derived work is distributed under the terms of a permission notice identical to this one.Permission is granted to copy and distribute translations of this manual into another language, under the above conditions for modified versions.
https://octave.org/doc/interpreter/Terminal-Input.html