From the octave CLI or octave GUI, if I run
plot([1,2,3],[1,4,9])
it will display a plot window that I can look at and interact with. If however I create file myPlot.m with the same command as content
plot([1,2,3],[1,4,9])
and that I run it with
octave myPlot.m
then I can briefly see the plot window appear for a fraction of a second and immediatly close itself. How can I prevent this window from closing itself?
Octave 4.2.2 Ubuntu 18.04
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With