从c#启动gnuplot,更改目录并加载多个gnuplot脚本

从c#启动gnuplot,更改目录并加载多个gnuplot脚本,c#,gnuplot,C#,Gnuplot,我想在C#应用程序中生成数百个.txt脚本,启动GnuPlot并为我拥有的每个脚本生成.png图 我可以使用以下代码从C#启动GnuPlot: Process gnuPlotProcess = new Process(); gnuPlotProcess.StartInfo = new ProcessStartInfo(@"C:\Program Files\gnuplot\bin\wgnuplot.exe"); gnuPlotProcess.Start(); 当我试图更改当前目录,并在启动进程之

我想在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没有启动

第二个问题,我能够在GnuPlot的默认当前目录下测试,是在尝试传递“load'script_xxx.txt'”命令时。以下是完整的代码(灵感来源):

pathToScript上的脚本应该创建一个.png文件,当直接从gnuPlot启动时,它可以工作。但从代码来看,什么都不会发生


任何帮助都将不胜感激。

我希望此代码将帮助您:

        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))
        {
        }
int N=1000;
字符串dataFile=“data.txt”;//一个数据文件
字符串gnuplotScript=“gnuplotScript.plt”;//gnuplot脚本
字符串pngFile=“traction.png”;//输出png文件
//初始值
双x=0,y=0;
//要绘制的随机值
随机rnd=新随机();
StreamWriter sw=新StreamWriter(数据文件);
//美国产出标准
Thread.CurrentThread.CurrentCulture=CultureInfo.GetCultureInfo(“en-US”);
//生成用于可视化的数据
对于(int i=0;i

你可以用你自己的数据重复这个例子,只要你想制作很多png文件,我希望这段代码能帮助你:

        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))
        {
        }
int N=1000;
字符串dataFile=“data.txt”;//一个数据文件
字符串gnuplotScript=“gnuplotScript.plt”;//gnuplot脚本
字符串pngFile=“traction.png”;//输出png文件
//初始值
双x=0,y=0;
//要绘制的随机值
随机rnd=新随机();
StreamWriter sw=新StreamWriter(数据文件);
//美国产出标准
Thread.CurrentThread.CurrentCulture=CultureInfo.GetCultureInfo(“en-US”);
//生成用于可视化的数据
对于(int i=0;i
您可以使用自己的数据重复此示例,次数与您想要制作多个png文件的次数相同