How to take input in Octave GNU?
Octave is open-source, free available for many of the platforms. It is a high-level language. It comes up with a text interface along with an experimental graphical interface. It is also used for various Machine Learning algorithms for solving various numeric problems. You can say that it is similar to MATLAB but slower than MATLAB.
There are multiple library functions to take input from the user in Octave.
input()
The input()
function prints the prompt and waits for the user to enter a value, it takes an expression, evaluates it, and then returns it. The return arguments depend on the expression entered.
Syntax : input(prompt, “s”)
Parameters :
- prompt : the text prompted on the terminal
- “s” : indicates not to evaluate the entered expression
Returns : depends on the input value
Example 1 :
var = input( "Enter an expression : " ); % entered expression is 2 + 3 fprintf( "Input is %d\n" , var); |
Output :
Enter an expression : 2 + 3 Input is 5
Example 2 : Using the “s” attribute :
var = input( "Enter an expression : " , "s" ); % entered expression is 2 + 3 fprintf( "Input is %s\n" , var); |
Output :
Enter an expression : 2 + 3 Input is 2 + 3
yes_or_no()
The yes_or_no() function accepts only two input values, either yes or no.
Syntax : yes_or_no(“prompt”)
Parameters :
- prompt : the text prompted on the terminal
Returns : 1 if “yes”, 0 if “no”
If any other value except yes or no is entered, the prompt will reappear asking for the input.
Example :
var = yes_or_no( "Enter a value : " ); % entered value is yes disp(var) var = yes_or_no( "Enter a value : " ); % entered value is no disp(var) |
Output:
Enter a value : (yes or no) yes 1 Enter a value : (yes or no) no 0
kbhit()
The kbhit()
function waits for any keypress, after the key is pressed, it returns that key.
Syntax : kbhit(argument)
Parameters :
- argument : if called with an argument, it does not wait for a keypress
Returns : depends on the input value
Example :
kbhit() % entered value is a kbhit(1) |
ans = a ans =
menu()
The menu()
function is used to display a menu with a heading title and options, and wait for the user input. If the GUI is running, the menu is displayed graphically Otherwise, the title and menu options are printed on the console.
Syntax : menu(title, opt1, …)
Parameters :
- title : title of the menu window
- opt1, opt2 … : list of options in the menu window
Returns : depends on the option selected
Example :
% generating the menu choice = menu( "title" , "opt1" , "opt2" , "opt3" ); % displaying the choice fprintf( "The choice is : " ); disp(choice); |
Output :
The choice is : 1
Contact Us