19

In Octave I can suppress or hide the output of an instruction adding a semicolon to the end of a line:

octave:1> exp([0 1])
ans = [ 1.0000   2.7183 ]
octave:2> exp([0 1]);
octave:3> 

Now, how can I suppress the output if the function displays text (e.g. using disp() or print()) before returning its value? In other words, I want to be able to do this:

disp("Starting...");
% hide text the may get displayed after this point
% ...
% show all text again after this point
disp("Done!");
andersonvom
  • 11,701
  • 4
  • 35
  • 40

3 Answers3

15

You can modify the PAGER variable (which is now a function) to redirect standard output. On Unix systems, you can redirect it to /dev/null. On Windows, I tried simply redirecting to a Python program that does nothing, and it works decently. (Basically, any program that ignores the input will do)

PAGER('/dev/null');
page_screen_output(1);
page_output_immediately(1);

You can just change it back after you're done. And maybe encapsulate this whole procedure in a function.

oldpager = PAGER('/dev/null');
oldpso = page_screen_output(1);
oldpoi = page_output_immediately(1);

% Call function here

PAGER(oldpager);
page_screen_output(oldpso);
page_output_immediately(oldpoi);

You can also simply run your scripts non-interactively, and redirect the output normally.

octave script.m > /dev/null
voithos
  • 68,482
  • 12
  • 101
  • 116
  • This has the disadvantage of disabling all text display, and I would like to be able to hide text from a single method but still being able to display my own text. – andersonvom Dec 08 '11 at 23:31
  • Just change it back after you call the function. I gave an example. There's no other real convenient way of accomplishing this, that I know of. – voithos Dec 08 '11 at 23:46
3

It's a very old question, but still, I've encountered the same problem and here is the trick that can help. One can use evalc to wrap a problematic function call. E.g. you have a code:

[a, b] = verbose_func(x,y);

Now you can do it:

evalc('[a, b] = verbose_func(x,y)');

and make it silent.

Funny, but it even works with other eval inside. I mean we can have:

code_str = '[a, b] = verbose_func(x,y)';
eval(code_str);

which is verbose. Now:

code_str = '[a, b] = verbose_func(x,y)';
evalc('eval(code_str)');

and this is not.

Hero Qu
  • 911
  • 9
  • 10
3

A quick hack of your problem and maybe not even worth mentioning is overloading the disp function like so:

function disp(x)
end

Then the original disp function is not called but yours instead in which no output is generated.

I also tried to somehow redirect stdout of octave, but unsuccessful. I hope that this dirty solution maybe will suffice in your situation^^

Woltan
  • 13,723
  • 15
  • 78
  • 104
  • This has the problem of making the `disp` function completely unavailable, even for my own use. Plus, a given function may make use of other methods to display text (e.g. print, sprintf, printf, etc) – andersonvom Dec 08 '11 at 23:29
  • @andersonvom I agree. It is as I mentioned a quick hack. Though you could still make use of the `disp` method if you use a second parameter like `function disp(x, y) if nargin > 1 printf("%d", y)`. Still it remains a hack, and I am still interested in *nice* answer^^. – Woltan Dec 12 '11 at 10:23
  • overloading disp does not stop output from printf or omitting ; – mathreadler Nov 09 '15 at 22:15