如何在 运行 循环时读取 Octave 中的键盘输入?
How to read keyboard inputs in Octave while running loop?
问题:
我是 运行 一个包含无限循环的脚本,我想通过用户输入退出此循环。我不想使用标准的 "input" 函数,因为它会在等待用户输入时暂停循环的执行。我希望程序一直循环(直到给出某些特定的键盘输入)。我也不想用 ctrl+c 退出循环,因为程序关闭了位于循环之后的程序,不会执行。
问题:
在 Octave 中,当在脚本执行期间向命令 window 键入内容时,命令 window 中不会显示任何内容,直到脚本结束。由此可以清楚地看出,在脚本执行期间给出的键盘输入存储在某个地方(对吗?)。而现在的大客在哪里?我如何访问这些数据?
我是 运行 Win7 中的 Octave 4.0.0
p.s。也欢迎其他停止循环的建议
使用 kbhit:
while (1)
if (kbhit (1) == 'x')
break
endif
sleep (0.2)
printf ("Loop is running...\n");
fflush (stdout);
endwhile
或者,如果您想使用 ctrl-c 退出并完成您的脚本,请使用 unwind_protect、unwind_ptrotect_cleanup 块
unwind_protect
while (1)
sleep (0.2)
printf ("Loop is running...\n");
fflush (stdout);
endwhile
unwind_protect_cleanup
disp ("doing my cleanup");
end_unwind_protect
In octave, when something is typed to the command window during execution of a script nothing is shown in the command window until the script has ended. From this it is clear that keyboard inputs that are given during the execution of a script are stored somewhere (is this right?). And now the big guestion is where? And how can I access this data?
这称为缓冲,是一种非常常见的行为。原理很简单。您的系统不会在准备就绪时写入所有内容,而是将其保存在缓冲区中,并且仅在被告知或缓冲区已满时才写入。许多磁盘操作都是这样工作的,例如,当您将一些小文件复制到 U 盘中时(取决于挂载选项)。一旦缓冲区已满,或者当你点击弹出U盘时,系统就会真正执行写入。
如果您阅读 Octave 手册的 Paging Screen Output 部分,您将看到:
Normally, no output is displayed by the pager until just before Octave is ready to print the top level prompt, or read from the standard input (for example, by using the fscanf or scanf functions). This means that there may be some delay before any output appears on your screen if you have asked Octave to perform a significant amount of work with a single command statement. The function fflush may be used to force output to be sent to the pager (or any other stream) immediately.
或者,您可以使用 page_output_immediately()
:
关闭此缓冲
Query or set the internal variable that controls whether Octave sends output to the pager as soon as it is available.
Otherwise, Octave buffers its output and waits until just before the prompt is printed to flush it to the pager.
问题:
我是 运行 一个包含无限循环的脚本,我想通过用户输入退出此循环。我不想使用标准的 "input" 函数,因为它会在等待用户输入时暂停循环的执行。我希望程序一直循环(直到给出某些特定的键盘输入)。我也不想用 ctrl+c 退出循环,因为程序关闭了位于循环之后的程序,不会执行。
问题:
在 Octave 中,当在脚本执行期间向命令 window 键入内容时,命令 window 中不会显示任何内容,直到脚本结束。由此可以清楚地看出,在脚本执行期间给出的键盘输入存储在某个地方(对吗?)。而现在的大客在哪里?我如何访问这些数据?
我是 运行 Win7 中的 Octave 4.0.0
p.s。也欢迎其他停止循环的建议
使用 kbhit:
while (1)
if (kbhit (1) == 'x')
break
endif
sleep (0.2)
printf ("Loop is running...\n");
fflush (stdout);
endwhile
或者,如果您想使用 ctrl-c 退出并完成您的脚本,请使用 unwind_protect、unwind_ptrotect_cleanup 块
unwind_protect
while (1)
sleep (0.2)
printf ("Loop is running...\n");
fflush (stdout);
endwhile
unwind_protect_cleanup
disp ("doing my cleanup");
end_unwind_protect
In octave, when something is typed to the command window during execution of a script nothing is shown in the command window until the script has ended. From this it is clear that keyboard inputs that are given during the execution of a script are stored somewhere (is this right?). And now the big guestion is where? And how can I access this data?
这称为缓冲,是一种非常常见的行为。原理很简单。您的系统不会在准备就绪时写入所有内容,而是将其保存在缓冲区中,并且仅在被告知或缓冲区已满时才写入。许多磁盘操作都是这样工作的,例如,当您将一些小文件复制到 U 盘中时(取决于挂载选项)。一旦缓冲区已满,或者当你点击弹出U盘时,系统就会真正执行写入。
如果您阅读 Octave 手册的 Paging Screen Output 部分,您将看到:
Normally, no output is displayed by the pager until just before Octave is ready to print the top level prompt, or read from the standard input (for example, by using the fscanf or scanf functions). This means that there may be some delay before any output appears on your screen if you have asked Octave to perform a significant amount of work with a single command statement. The function fflush may be used to force output to be sent to the pager (or any other stream) immediately.
或者,您可以使用 page_output_immediately()
:
Query or set the internal variable that controls whether Octave sends output to the pager as soon as it is available.
Otherwise, Octave buffers its output and waits until just before the prompt is printed to flush it to the pager.