0

Context: When I use Python + matplotlib, I can compose the code in any text editor (like Sublime Text), do CTRL+B, and then the text output appears in the "Build results" panel of the text editor, and, optionally, graph/plots are rendered in a new GUI window.
Under the hood, the text editor calls python myscript.py when we do "Build", and that's it.

It's simple and working flawlessly, easily.


Now I'm trying to do the same with GNU Octave: write a test.m code (such as this one). Then run it from my favorite text editor (and not the Octave IDE), or from command-line. I tried:

  • octave test.m: the plot is displayed during 100 ms and then disappears! not OK

  • octave --persist test.m: the plot stays displayed, this is ok ... but this part is not good: it opens an IDE (which I don't want since I want to continue using my usual text editor!), see the background window:

    enter image description here

How to have GNU Octave behave like expected: the text output in stdout (either in terminal or in the "Build Results" panel of your text editor) and the plot output in a new window? Important: without spawning an IDE window.

I find strange that this behaviour is not the default behaviour. Shouldn't it be?


Edit: solved with:

octave-cli test.m

and

k = plot(...)
waitfor(k)
Basj
  • 41,386
  • 99
  • 383
  • 673
  • What happens when you try to run those 2 octave commands from CMD? – MattDMo Feb 04 '21 at 21:48
  • Exactly what I described in the question (I did the tests from command line) – Basj Feb 04 '21 at 22:36
  • to have a similar effect I run the command line interface `octave-cli` in a terminal and `vim` in a different one. Not exactly what you are looking for, but matplotlib is a python module while octave is a separate program from your editor. – matzeri Feb 05 '21 at 06:18
  • Thank you, I'll try it, can you post an answer with this @matzeri? – Basj Feb 05 '21 at 06:46
  • Your question has nothing to do with running scripts outside of the IDE. It's a question on how to prevent the plot window from closing when a script exits. It is a duplicate of this question: https://stackoverflow.com/a/52717469/4183191 – Tasos Papastylianou Feb 05 '21 at 11:54
  • I also feel compelled to point out that this is exactly the kind of thing that does _not_ work "out of the box" in python. You are simply more familiar with the process for doing so in python. In python, in reality, you first need to ensure your python script is a script vs called as a script from the interpreter vs called as a module, _then_ decide whether the plot should block or not. I.e. exactly the same thought process as octave, just different function names involved. And this is not even a critique of python, both languages are great, that's just the normal way of doing things. – Tasos Papastylianou Feb 05 '21 at 11:59
  • 1
    @TasosPapastylianou Thank you for your help, it solved it indeed! PS: Python matplotlib `plt.show()` is blocking by default (which is great), so that's why I was saying it works "out-of-the-box". – Basj Feb 05 '21 at 12:29
  • @Basj yes, indeed. This brings another nice point about the difference between python and octave, and how you shouldn't be trying to replicate your workflow from one to the other identically. In python, the expectation is that running a script is done outside the REPL, since scripts are in fact modules, and re-importing a module doesn't guarantee re-running it as your would expect a script. Therefore, blocking as default makes sense, because you don't want your OS to exit. Conversely, octave scripts are typically run interactively inside REPL sessions. Therefore blocking is off by default. :) – Tasos Papastylianou Feb 05 '21 at 13:33
  • in other words, you will find that if you had gone the other way round, and tried to replicate your octave workflow identically into python, you'd find that octave "works out of the box", whereas python doesn't! I think the workflow you should be going for in octave is one where you are editing your script in the editor on one (terminal) window, and you launch it from within a "live" octave session to test it as you make changes. The main reason one doesn't use this workflow in python is because the python REPL doesn't really help you to test a changing file from the same session like that. – Tasos Papastylianou Feb 05 '21 at 13:34
  • Thanks for this information @TasosPapastylianou. I'll vote to reopen this question, I think it would be very interesting - for future reference - that you agglomerate all your past comments into an answer. It gives the big picture / philosophy / difference between REPL / non-REPL, and this is very valuable information. (Comments tend to be cleaned after days/weeks/months, so an answer with your comments would be really better). – Basj Feb 05 '21 at 15:39
  • In that case, I voted to re-open, though it might not happen. Even if not however, I wouldn't worry too much about comments cleaned up; the question has an upvoted accepted answer, so it's not likely to go anywhere :) – Tasos Papastylianou Feb 05 '21 at 18:46

1 Answers1

2

to have a similar effect I run the command line interface octave-cli or octave --no-gui in a terminal and vim in a different one.
Not exactly what you are looking for, but matplotlib is a python module while octave is a separate program from your editor.

See example of the two terminals and the graphs, all on separate windows.

enter image description here

matzeri
  • 8,062
  • 2
  • 15
  • 16
  • Thanks! For me, `octave-cli test.m` didn't work (`warning: function .\test.m shadows a core library function`), `octave --no-gui test.m` immediately returns (we can see the plot during 10 milliseconds maybe). But `octave --no-gui --persist test.m` works. And doesn't load the IDE, which is already great! Unfortunately, I'd prefer that the Octave console quits when we close the plot window (like when using `python myscript.py` with a script using matplotlib). Is that possible? That way, building multiple times from the text editor with CTRL+B wouldn't spawn 10 different Octave consoles – Basj Feb 05 '21 at 08:18
  • instead of running the command from the Editor, I will use `run test.m` from the Octave console – matzeri Feb 05 '21 at 08:32