Sriram
Sriram

Reputation: 10558

Getting octave to plot when invoking a function from the command line

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?

Upvotes: 7

Views: 12429

Answers (6)

Dovydas Laurisonis
Dovydas Laurisonis

Reputation: 11

Also can try wait for key.

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

Upvotes: 1

crow
crow

Reputation: 305

You can use:

waitfor(h)

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

Upvotes: 8

Anne van Rossum
Anne van Rossum

Reputation: 3149

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

Upvotes: 3

psihodelia
psihodelia

Reputation: 30492

Just use pause after your plotting functions

Upvotes: 12

cleek
cleek

Reputation: 21

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 :)

Upvotes: 2

Woltan
Woltan

Reputation: 14023

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")

Upvotes: 4

Related Questions