【问题标题】:Problem with Invoking Octave commands from c#从 c# 调用 Octave 命令的问题
【发布时间】:2019-04-24 00:06:13
【问题描述】:

我正在尝试使用 Octave 的 surf 功能从我的 c# 应用程序中绘制 3d 数据。

我使用 Process.Start() 来启动 Octave 和 Process.WriteLine() 来发送命令 同样的方法可以很好地使用 gnuplot 进行绘图,但它不适用于 Octave

Process octavePro;
        StreamWriter octaveStWr;
        string octavepath = @"C:\Octave\Octave-5.1.0.0\mingw64\bin\octave-cli-5.1.0.exe";
    private void button1_Click(object sender, EventArgs e)
    {
        octavePro = new Process();
        octavePro.StartInfo.FileName = octavepath;
        octavePro.StartInfo.UseShellExecute = false;
        octavePro.StartInfo.RedirectStandardInput = true;
        octavePro.StartInfo.RedirectStandardOutput = true;
        octavePro.StartInfo.RedirectStandardError = true;
        octavePro.StartInfo.CreateNoWindow = false;
        octavePro.OutputDataReceived += new DataReceivedEventHandler(InterProcOutputHandler);
        octavePro.ErrorDataReceived += new DataReceivedEventHandler(InterProcOutputHandler);
            octavePro.Start();
            octaveStWr = octavePro.StandardInput;
            Thread.Sleep(100);
            octaveStWr.WriteLine("info");
            octaveStWr.WriteLine("surf([1 2 3; 2 3 4; 3 4 5])");
            octaveStWr.Flush();
        }

当我在 Octave-CLI 中执行“info”和“surf([1 2 3; 2 3 4; 3 4 5])”这两个命令时,我分别获得了应用程序信息和数据的 3d 图。 但是当我尝试从 C# 应用程序中执行它时,只启动了一个终端窗口,并且窗口中没有显示任何内容。

【问题讨论】:

    标签: c# octave


    【解决方案1】:

    您可能需要增加 Octave 初始化的休眠时间。根据每个命令的持续时间,您可能需要添加更多睡眠。也可以使用waitfor 来暂停 Octave 的执行,直到图窗关闭。

    octavePro.Start();
    octaveStWr = octavePro.StandardInput;
    octavePro.BeginOutputReadLine();
    octavePro.BeginErrorReadLine(); 
    Thread.Sleep(5000);
    octaveStWr.WriteLine("info");
    Thread.Sleep(1000);
    octaveStWr.WriteLine("waitfor(surf([1 2 3; 2 3 4; 3 4 5]))");
    

    【讨论】:

    • 感谢信息丰富的回答“waitfor”解决了这个问题。但是如果我需要在不关闭图形的情况下实时更新绘图呢?
    • 带有“waitfor”函数的超时属性,最小超时为1秒。这限制了刷新率。有什么办法可以提高刷新率吗?
    • 抱歉回复晚了。您可以使用uiwait。考虑以下示例:octaveStWr.WriteLine("for i=1:10;imshow(rand(100),[]);drawnow;end;");octaveStWr.WriteLine("uiwait()");
    猜你喜欢
    • 2021-12-27
    • 1970-01-01
    • 2023-04-01
    • 2020-10-24
    • 2021-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多