1

C# アプリで数百の .txt スクリプトを生成し、GnuPlot を起動して、各スクリプトの .png グラフを生成したいと考えています。

次のコードを使用して、C# から GnuPlot を起動できます。

Process gnuPlotProcess = new Process();
gnuPlotProcess.StartInfo = new ProcessStartInfo(@"C:\Program Files\gnuplot\bin\wgnuplot.exe");
gnuPlotProcess.Start();

最初の問題は、現在のディレクトリを変更しようとしているときに発生し、プロセスを開始する前に次のコード行を追加します。

gnuPlotProcess.StartInfo.Arguments = "cd '" + scriptsPath + "'";

GnuPlot が起動しなくなりました。

2 つ目の問題は、GnuPlot のデフォルトの現在のディレクトリでテストできましたが、"load 'script_xxx.txt'" コマンドを渡そうとしたときです。完全なコードは次のとおりです ( C# の Gunplot からのプロット グラフからのインスピレーション):

Process gnuPlotProcess = new Process();
gnuPlotProcess.StartInfo = new ProcessStartInfo(@"C:\Program Files\gnuplot\bin\wgnuplot.exe");
gnuPlotProcess.StartInfo.RedirectStandardInput = true;
gnuPlotProcess.StartInfo.UseShellExecute = false;
theProcess.Start();
StreamWriter sw = gnuPlotProcess.StandardInput;
sw.WriteLine("load '" + pathToScript + "'");
sw.Flush();

pathToScript にあるスクリプトは .png ファイルを作成する必要があり、gnuPlot から直接起動すると機能します。しかし、コードからは何も起こりません。

どんな助けでも大歓迎です。

4

1 に答える 1

1

このコードがお役に立てば幸いです:

        int N = 1000;                                    
        string dataFile = "data.txt";                  // one data file
        string gnuplotScript = "gnuplotScript.plt";    // gnuplot script
        string pngFile = "trajectory.png";             // output png file

        // init values
        double x = 0, y = 0;

        // random values to plot
        Random rnd = new Random();
        StreamWriter sw = new StreamWriter(dataFile);

        // US output standard
        Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo("en-US");

        // generate data for visualisation       
        for (int i = 0; i < N; i++)
        {
            x += rnd.NextDouble() - 0.5;
            y += rnd.NextDouble() - 0.5;
            sw.WriteLine(x.ToString("F3") + "\t" + y.ToString("F3"));
        }
        sw.Close();

        // you can download it from file
        string gnuplot_script = "set encoding utf8\n" +
                                "set title \"Random trajectory\"\n" +
                                "set xlabel \"Coordinate X\"\n" +
                                "set ylabel \"Coordinate Y\"\n" +
                                "set term pngcairo size 1024,768 font \"Arial,14\"\n" +
                                "set output \"pngFile\"\n" +
                                "plot 'dataFile' w l notitle\n" +
                                "end";

        // change filenames in script
        gnuplot_script = gnuplot_script.Replace("dataFile", dataFile);
        gnuplot_script = gnuplot_script.Replace("pngFile", pngFile);

        // write sccript to file
        sw = new StreamWriter(gnuplotScript, false, new System.Text.UTF8Encoding(false));
        sw.WriteLine(gnuplot_script);
        sw.Close();

        // launch script
        ProcessStartInfo PSI = new ProcessStartInfo();
        PSI.FileName = gnuplotScript;
        string dir = Directory.GetCurrentDirectory();
        PSI.WorkingDirectory = dir;
        using (Process exeProcess = Process.Start(PSI))
        {
            exeProcess.WaitForExit();
        }

        // OPTION: launch deafault program to see file
        PSI.FileName = pngFile;
        using (Process exeProcess = Process.Start(PSI))
        {
        }

この例を独自のデータで何度でも繰り返して、多くの png ファイルを作成できます。

于 2013-07-18T12:31:27.310 に答える