简体   繁体   English

如何在运行m文件后退出GNU Octave,而不关闭绘图窗口?

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

I have been writing a C++ program to solve the problem of the simple pendulum and then plot the result using GNU Octave. 我一直在编写一个C ++程序来解决简单摆的问题,然后使用GNU Octave绘制结果。 It plots the result via this line in my program: 它在我的程序中通过这一行绘制结果:

system("./simppenadj.sh");

where simppenadj.sh is: simppenadj.sh在哪里:

#!/bin/sh
octave --no-gui --persist -q simppenadj.m

and simppenadj.m is: simppenadj.m是:

#!/usr/bin/octave
# Plotting simppenadj.txt
A      = importdata('simppenadj.txt');
B      = importdata('simppenadjdx.txt');
t      = A(:,1);
theta  = A(:,2);
dtheta = B(:,2);
figure
plot(t,theta)
xlabel('t','FontSize',16,'FontWeight','bold')
ylabel('\theta','FontSize',16,'FontWeight','bold')
title('{d^{2}\theta}/{d{t^{2}}} = -9.8 cos({\theta})','FontSize',18,'FontWeight','bold')
figure
plot(theta,dtheta)
xlabel('\theta','FontSize',16,'FontWeight','bold')
ylabel('d\theta/dt','FontSize',16,'FontWeight','bold')
title('{d^{2}\theta}/{d{t^{2}}} = -9.8 cos({\theta})','FontSize',18,'FontWeight','bold')

Whenever I run my C++ program the CLI of GNU Octave is started (and left opened at the end) and the data is plotted. 每当我运行我的C ++程序时,GNU Octave的CLI就会启动(并在结尾处保持打开状态)并绘制数据。 I do not want the CLI of GNU Octave to be left open, but the only way I know how to get it not to open is to remove the --persist option in simppenadj.sh which also makes the plots generated by GNU Octave to not be left open. 我不想悬空GNU八度的CLI,但只有这样,我知道如何得到它不打开是除去--persist在选项simppenadj.sh这也使得由GNU八度,以不产生地块保持开放。 This is a problem, as I want the plots to be left opened after my C++ program has been run. 这是一个问题,因为我希望在我的C ++程序运行后打开这些图。 So is there a way to do this? 有没有办法做到这一点?

You can use the octave API to call the script from within your program. 您可以使用octave API从程序中调用脚本。 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 . 使用此方法,没有八度音阶CLI,因为您通过API执行所有对八度音程的调用,尤其是feval

Unfortunately, the guide on using the API is very bad, but i put something together for you which should work. 不幸的是,关于使用API​​的指南非常糟糕,但我为你准备了一些应该可行的东西。 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. 我在octave文件中添加了printf语句,以显示如何将参数传递给octave。

main.cpp 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 编译main.cpp

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. 您也可以使用mkoctfile命令,它基本上也是这样。 You can look at the output of its -p switch, eg 您可以查看其-p开关的输出,例如

mkoctfile -p CFLAGS

to get the libs, compiler flags etc. Have a look at the manpage for this. 获取libs,编译器标志等。看看这个的联机帮助页。

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. 保持持久会导致八度提示,这会阻止c ++程序的其余部分运行直到退出。
  • Removing persist closes the figures, but then the rest of your c++ program runs normally. 删除持久性将关闭数字,但随后c ++程序的其余部分将正常运行。

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. 假设你在Linux终端上,这更像是一个黑客,但你可以在你的八度音文件上添加一个暂停命令(实际上是一个小暂停,如果你想确保所有图像被刷新,则无限期暂停),然后从最后用“&”调用c ++中的脚本,这使它进入“守护进程”模式。

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. 结果是八度代码将在后台运行,这意味着你的情节保持不变,你的c ++程序不会停止。

Having said that, the plots will close once your c++ program exits. 话虽如此,一旦你的c ++程序退出,这些情节就会关闭。 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. 如果您不希望这样,并且您希望即使在您的c ++程序“完成”之后图也保持打开,那么您也可以使用c ++代码末尾的pause()或sigsuspend()函数来进一步破解。 ,然后用&调用你的c可执行文件。 (and once you're done playing, kill it manually from your terminal, or just exit your terminal) (一旦你完成游戏,从你的终端手动杀死它,或者只是退出你的终端)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM