Octave: How to prevent plot window from closing itself?

我的梦境 提交于 2019-11-27 08:04:46

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/52569584/octave-how-to-prevent-plot-window-from-closing-itself

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!