How to exit GNU Octave, after running an m file, without closing plot windows?

℡╲_俬逩灬. 提交于 2019-12-01 20:37:49

You can use the octave API to call the script from within your program. There, create a child process, which calls octave, so the parent process can end. With this, you can keep octave running. With this method, there is no octave CLI, since you do all calls to octave via the API, especially feval.

Unfortunately, the guide on using the API is very bad, but i put something together for you which should work. It basically only reads a script and executes the corresponding function. This is the nice thing about this method: you can write everything using normal octave function/script file methods.

I added the printf statement in the octave file in order to show how you can pass arguments to octave.

main.cpp

#include <iostream>

#include <unistd.h>

#include <octave/oct.h>
#include <octave/octave.h>
#include <octave/parse.h>
#include <octave/toplev.h>

int main()
{
    pid_t pid = fork();

    if(pid != 0) // parent
    {
        std::cout << "parent, exiting\n";
    }
    else
    {
        // arguments for octave
        string_vector argv (2);
        argv(0) = "embedded";
        argv(1) = "-q"; // quiet

        // start octave, run embedded (third parameter == true)
        octave_main (2, argv.c_str_vec (), true);

        // read the script file
        source_file("calc_and_plot.m");

        // call the function with an argument
        octave_value_list in;
        in(0) = "Hello, world.";
        feval("calc_and_plot", in);


        std::cout << "octave (child process) done\n";
        clean_up_and_exit(0); // quit octave. This also quits the program,
                              // so use this together with atexit, if you 
                              // need to do something else after octave exits
    }

    return 0;
}

octave script/function file

function calc_and_plot(str)
    printf('%s\n', str);
    x = linspace(0, 2*pi, 100);
    y = sin(x);
    it = plot(y);
    waitfor(it);
end

Compile the main.cpp with

g++ main.cpp -L/usr/lib/octave-4.0.2 -I/usr/include/octave-4.0.2 -loctave -loctinterp

You have to adjust the paths to your system and octave version. You can also use the mkoctfile command, which basically does the same. You can look at the output of its -p switch, e.g.

mkoctfile -p CFLAGS

to get the libs, compiler flags etc. Have a look at the manpage for this.

I'm assuming you have the following problem:

  • Keeping persist results in an octave prompt, which stops the rest of your c++ program from running until you exit.
  • Removing persist closes the figures, but then the rest of your c++ program runs normally.

Assuming you're on a linux terminal, this is more of a hack, but you could just add a pause command on your octave file (actually a small pause followed by an indefinite pause if you want to make sure all images are flushed), and then call your script from c++ with a "&" at the end, which makes it go into 'daemon' mode.

The result of this is that the octave code will run in the background, meaning your plots stay up, and your c++ program doesn't halt.

Having said that, the plots will close once your c++ program exits. If you don't want that either and you want the plots to stay open even after your c++ program has "finished", then you can hack even further using a pause() or sigsuspend() function at the end of your c++ code too, and then call your c executable with a & too. (and once you're done playing, kill it manually from your terminal, or just exit your terminal)

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