问题
Is there a command like 'scanf' in GNU Octave to read the user inputs from the keyboard?
回答1:
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.
回答2:
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:
5 66 7
The program responds:
line = 5 6 7
Read more about function: fgetl
https://www.gnu.org/software/octave/doc/interpreter/Line_002dOriented-Input.html#XREFfgetl
回答3:
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)
来源:https://stackoverflow.com/questions/4882706/how-can-we-get-user-inputs-from-the-keyboard-in-gnu-octave