Octave: How to prevent plot window from closing itself?

Run @terminal as (need to exit octave later)

octave --persist myscript.m

or append

waitfor(gcf)

at the end of script to prevent plot from closing


Here is a full example, given the confusion in the comments.

Suppose you create a script called plotWithoutExiting.m, meant to be invoked directly from the linux shell, rather than from within the octave interpreter:

#!/opt/octave-4.4.1/bin/octave

h = plot(1:10, 1:10);
waitfor(h)
disp('Now that Figure Object has been destroyed I can exit')

The first line in linux corresponds to the 'shebang' syntax; this special comment tells the bash shell which interpreter to run to execute the script below. I have used the location of my octave executable here, yours may be located elsewhere; adapt accordingly.

I then change the permissions in the bash shell to make this file executable

chmod +x ./plotWithoutExiting.m

Then I can run the file by running it:

./plotWithoutExiting.m

Alternatively, you could skip the 'shebang' and executable permissions, and try to run this file by calling the octave interpreter explicitly, e.g.:

octave ./plotWithoutExiting.m

or even

octave --eval "plotWithoutExiting"

You can also add the --no-gui option to prevent the octave GUI momentarily popping up if it does.

The above script should then run, capturing the plot into a figure object handle h. waitfor(h) then pauses program flow, until the figure object is destroyed (e.g. by closing the window manually).

In theory, if you did not care to collect figure handles, you can just use waitfor(gcf) to pause execution until the last active figure object is destroyed.

Once this has happened, the program continues normally until it exits. If you're not running the octave interpreter in interactive mode, this typically also exits the octave environment (you can prevent this by using the --persist option if this is not what you want).

Hope this helps.