40

I have a program in Octave that has a loop - running a function with various parameters, not something that I can turn into matrices. At the beginning of each iteration I print the current parameters using 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?

2

7 Answers 7

46

Use fflush(stdout) and/or fflush(stderr) to flush the buffer from disp().

Sign up to request clarification or add additional context in comments.

2 Comments

Out of curiosity, do you know if there's a Matlab equivalent to fflush()? I'd like to do the same thing, but I use Matlab, not Octave, and there's no fflush() function.
In MATLAB, use drawnow('update') to flush the output.
30

As mentioned by moastab, fflush(stdout) works for Octave.

In MATLAB, use drawnow('update') to flush the output.

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. The 'update' option is not required, but limits the flushing to non-graphical queues. This detail is merely implied in the drawnow() documentation; I have verified it to work on fprintf calls in a loop.

Comments

21

Octave: You can turn off buffering of output by calling more off.

This will disable pagination such that all output is sent directly to the screen.

Comments

7

Put the following commands at the beginning of your section or your code:

page_screen_output(0);

page_output_immediately(1);

Comments

3

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 filename will use filename instead of default "diary".

It is build -in function in both Octave and MATLAB. For more details see help diary.


Also you can increase Octave buffer size. On Windows you can do it in Octave Properties dialog from upper left corner menu.

Comments

2

From here and elsewhere, there are at least 5 methods to get immediate output, in 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

Comments

0

drawnow will cause graphs to update, I'm not sure if it works on the stdout pipe as well.

You might also convert your disp(...) statements to fprintf(stderr, ...), I think stderr is handled differently from stdout on Octave.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.