2

While a script function is running, the results produced by fprintf or disp commands do not show in the terminal, only when the execution ends (or it is stopped) then they all appear. fflush solves the problem, but it's tedious to use after each print. Is there any way to auto-flush to the terminal?

Using version 3.8.1.

nightcod3r
  • 752
  • 1
  • 7
  • 26
  • Possible duplicate of [How can I flush the output of disp in Matlab or Octave?](http://stackoverflow.com/questions/2633019/how-can-i-flush-the-output-of-disp-in-matlab-or-octave) – Mad Physicist Jan 04 '16 at 03:43
  • Here, the discussion seems to point also towards graphics. My question is how to activate the auto-flush behavior that I used to have in former versions, and Matlab. – nightcod3r Jan 04 '16 at 03:55
  • The discussion in that question may appear to talk about graphics because the same function flushes graphical and io buffers in Matlab. I assure you it discusses the exact issue you are having. – Mad Physicist Jan 04 '16 at 11:33
  • I think you're right, the same answer has been given here. If there's no new contributions that could enrich the discussion in the other question, I will delete it. – nightcod3r Jan 04 '16 at 18:21

2 Answers2

4

To automatically flush stuff automatically, set:

page_output_immediately (1);

and to send it to stdout without a pager, set:

page_screen_output (0);

The Octave manual has a section on this subject, Paging Screen Output.

carandraug
  • 12,938
  • 1
  • 26
  • 38
2

You have two main options:

  1. Call fflush(stdout()) after each print.
  2. Print to stderr instead of stdout if your system auto-flushes stderr (e.g. Linux).

Option 1 is generally better because it is less system dependent. Either solution can easily be wrapped in your own print function using varargin:

function n = my_print(varargin)
    n = fprintf(stdout(), varargin{:});
    fflush(stdout());
end
Mad Physicist
  • 107,652
  • 25
  • 181
  • 264
  • That means typing annoying `fflush` commands all around. I'm using Linux, my original question was more on how to make the system auto-flush. I'll edit it accordingly. – nightcod3r Jan 04 '16 at 03:50
  • Printing to `stderr` solves the problem for the `fprintf` command in my system. Don't know if `disp` can also send to `stderr`, in principle, it does not work. – nightcod3r Jan 04 '16 at 04:01
  • 1
    I would suggest wrapping fprintf() in a function of your own to ensure that fflush() is called. This preserves the meaning of stderr and allows you to hide stdout if you wish by piping to null. – Ryan Tennill Jan 31 '18 at 20:23
  • @RyanTennill. That is an excellent suggestion. I have incorporated it in my answer. – Mad Physicist Jan 31 '18 at 20:42