简体   繁体   English

如何在Matlab或Octave中刷新disp的输出?

[英]How can I flush the output of disp in Matlab or Octave?

I have a program in Octave that has a loop - running a function with various parameters, not something that I can turn into matrices. 我在Octave中有一个程序有一个循环 - 运行一个带有各种参数的函数,而不是我可以变成矩阵的东西。 At the beginning of each iteration I print the current parameters using disp . 在每次迭代开始时,我使用disp打印当前参数。

The first times I ran it I had a brazillion warnings, and then I also got these prints. 我第一次跑它时有一个很棒的警告,然后我也得到了这些打印件。 Now that I cleaned them up, I no longer see them. 现在我清理了它们,我不再看到它们了。 My guess is that they're stuck in a buffer, and I'll see them when the program ends or the buffer fills. 我的猜测是他们被困在缓冲区中,当程序结束或缓冲区填满时我会看到它们。

Is there any way to force a flush of the print buffer so that I can see my prints? 有没有办法强制刷新打印缓冲区,以便我可以看到我的打印件?

使用fflush(stdout)和/或fflush(stderr)从disp()刷新缓冲区。

As mentioned by moastab, fflush(stdout) works for Octave. 正如moastab所提到的, fflush(stdout)适用于Octave。

In MATLAB, use drawnow('update') to flush the output. 在MATLAB中,使用drawnow('update')来刷新输出。

MATLAB's drawnow function will be familiar to those who control the redrawing of graphical objects in MATLAB, but it applies to the stdout stderr buffers as well. 对于在MATLAB中控制重绘图形对象的人来说,MATLAB的drawnow函数会很熟悉,但它也适用于stdout stderr缓冲区。 The 'update' option is not required, but limits the flushing to non-graphical queues. 'update'选项不是必需的,但会将刷新限制为非图形队列。 This detail is merely implied in the drawnow() documentation ; 这个细节仅仅隐含在drawnow()文档中 ; I have verified it to work on fprintf calls in a loop. 我已经验证它可以在循环中处理fprintf调用。

Octave : You can turn off buffering of output by calling more off . 八度 :您可以通过调用more off输出缓冲。

This will disable pagination such that all output is sent directly to the screen. 这将禁用分页,以便将所有输出直接发送到屏幕。

Put the following commands at the beginning of your section or your code: 将以下命令放在您的部分或代码的开头:

page_screen_output(0);

page_output_immediately(1);

If I understand your question correctly, you can use diary function to dump all session output to a text file. 如果我正确理解您的问题,您可以使用日记功能将所有会话输出转储到文本文件。 diary on will start recording, and diary off will stop. diary on将开始录音, diary off将停止。 diary filename will use filename instead of default "diary". diary filename名将使用文件名而不是默认的“日记”。

It is build -in function in both Octave and MATLAB. 它是Octave和MATLAB中的build -in函数。 For more details see help diary . 有关详细信息,请参阅help diary


Also you can increase Octave buffer size. 您还可以增加Octave缓冲区大小。 On Windows you can do it in Octave Properties dialog from upper left corner menu. 在Windows上,您可以从左上角菜单的“八度属性”对话框中执行此操作。

drawnow will cause graphs to update, I'm not sure if it works on the stdout pipe as well. drawnow会导致图形更新,我不确定它是否也适用于stdout管道。

You might also convert your disp(...) statements to fprintf(stderr, ...) , I think stderr is handled differently from stdout on Octave. 您也可以将您的disp(...)语句转换为fprintf(stderr, ...) ,我认为stderr的处理方式与Octave上的stdout不同。

From here and elsewhere, there are at least 5 methods to get immediate output, in Octave . 这里和其他地方,至少有5种方法可以在Octave中获得立即输出。

Use one of the following: 使用下列之一

%---------------------------
% Turn OFF output buffering
%---------------------------
more off                        % command & NOT shown in output
PAGER = "less"                  % built-in var - shown in output
page_screen_output = 0          % built-in var - shown in output
page_output_immediately = 1     % built-in var - shown in output
fflush(stdout)                  % Need to call after each "output" line

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM