7

I am trying to run a function in octave from the command line. The function is currently run like so:

octave --silent --persist --eval 'function(input arguments)'

function.m contains a plot command. When I invoke octave with the above command line parameters, the plot does show but octave enters into its interactive mode. My question is:

Is there any way to get octave to display the plot without entering the interactive mode when it is invoked from the command line?

Sriram
  • 10,298
  • 21
  • 83
  • 136

6 Answers6

12

Just use pause after your plotting functions

psihodelia
  • 29,566
  • 35
  • 108
  • 157
  • This is an excellent trick to give the program the cue and time to visualize the plotting instructions inside the figure, before running into further instructions (which may make the viewing difficult). I have used the variant `pause(N)` where N is the number of seconds to wait: in my case 0.1 served the purpose perfectly, – XavierStuvw Nov 20 '17 at 19:46
8

You can use:

waitfor(h)

at the end, which waits for you to close the figure.

crow
  • 305
  • 4
  • 13
  • 1
    This should be an actual answer. "waitfor(figure)" will wait with execution of script until the plot window is closed, therefore octave process will stay alive. – wirher May 12 '18 at 11:15
4

AFAIK, the plot window is a child process of octave and therefor can only be displayed when octave is running. Even if you plot something from the "interactive" mode leave the plot open and close octave, the plot will also disappear.
What you could do is to plot to some output file like posted here:

f = figure
set(f, "visible", "off")
plot([1,2,3,4])
print("MyPNG.png", "-dpng")
Community
  • 1
  • 1
Woltan
  • 13,723
  • 15
  • 78
  • 104
  • So you are saying there is no way to display the plot without getting into the interactive mode? – Sriram Jul 27 '11 at 11:26
  • @Sriram As far as I know there is no way. But I have not found any documentation that clearly states that... – Woltan Jul 27 '11 at 11:52
3

You need to select a proper graphics toolkit:

available_graphics_toolkits 
ans = 
{
  [1,1] = fltk
  [1,2] = gnuplot
}

The default is fltk which cannot write to file without displaying the plot. However, if you select gnuplot it will be able to write to file without displaying it first. In your file start with:

graphics_toolkit gnuplot
Anne van Rossum
  • 3,091
  • 1
  • 35
  • 39
2

The problem is that when you run from command line, when it ends, the plot windows disappear with it.

#! /usr/bin/octave -qf
f = figure;
set(f, "visible", "off")

t=0:0.001:5*pi;
plot(t, sin(5*t)), grid

print("MyPNG.png", "-dpng")

This saves output to MyPNG.png in the directory where it is run.

Then you might open it with a visualization program.

Another option is to add

pause

at the end of the program so it waits for user input to terminate, therefore to close the plot window.

Cheers :)

cleek
  • 21
  • 3
1

Also can try wait for key.

while (waitforbuttonpress ()==0) pause(1) end

  • 1
    Hi, this post does not seem to provide a [quality answer](https://stackoverflow.com/help/how-to-answer) to the question. Please either edit your answer and improve it, or just post it as a comment to the question/other answer. – sɐunıɔןɐqɐp Jun 12 '20 at 09:38