How can we get user inputs from the keyboard in GNU Octave?
Is there a command like 'scanf' in GNU Octave to read the user inputs from the keybo开发者_运维技巧ard?
Yes, the function is called input
. A simple example:
octave-3.2.4:3> x = input("Enter a number: ")
Enter a number: 25
x = 25
See the documentation for details, like overriding the default parsing behavior.
In GNU Octave, How to get user input line, aka open stdin:
Make a file called: test.m
Put this code in there:
line = fgetl(stdin);
line
Run it like so:
octave test.m
Enter in some words then press Enter
5 abc 7
The program responds:
line = 5 abc 7
Read more about function: fgetl
https://www.gnu.org/software/octave/doc/interpreter/Line_002dOriented-Input.html#XREFfgetl
There is scanf
function in addition to input
function.
For example:
% Get a number
x = scanf("%d", "C");
% Get a vector of size 5
for i=1:5
x(i) = scanf("%d", "C");
end
% Get a matrix
printf("Enter a 3x2 matrix \n ");
for i=1:3
for j=1:2
n(i,j) = scanf("%d", "C");
end
end
disp(n)
精彩评论