How can we get user inputs from the keyboard in GNU Octave?

醉酒当歌 提交于 2020-08-23 03:37:50

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!