Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plotting graph in GNU PLOT using C#

Tags:

c#

gnuplot

I have a dat file insert.dat

The contents are as follows:

1000    0.002322044 0.00291182
5000    0.000103257 0.000458963
10000   2.50E-05    0.000172019
20000   6.18E-06    6.03E-05
40000   2.51E-06    2.65E-05
60000   1.65E-06    1.71E-05
80000   1.21E-06    1.23E-05
100000  1.01E-06    9.97E-06

When I open the gnuplot.exe and type plot insert.dat with lines, I get a proper output but when I write a C# code as follows:

private  void GNUPlot()
{
    string pgm = @"E:\gnuplot\bin\gnuplot.exe";

    Process extPro = new Process();
    extPro.StartInfo.FileName = pgm;
    extPro.StartInfo.UseShellExecute = false;
    extPro.StartInfo.Standardization = true;
    extPro.Start();

    StreamWriter gnupStWr = extPro.StandardInput;
    gnupStWr.WriteLine("plot \"insert.dat\" with lines ");
    gnupStWr.Flush();
}

I get the following warning:

warning: Skipping unreadable file "insert.dat"

When I replace

gnupStWr.WriteLine("plot \"insert.dat\" with lines ");

with

gnupStWr.WriteLine("plot sin(x) ");

I get the required Sin(x) graph output.

insert.dat is in the current directory of gnuplot. I want insert.dat file data to be plotted.

like image 735
guddi Avatar asked Sep 24 '26 08:09

guddi


1 Answers

The problem seems to be that gnuplot cannot find the requested file. The working directory of the process is not set to the gnuplot directory, but probably to the directory of the application that is calling gnuplot.

EDIT Try to add either of the following lines to your code somewhere before the extPro.Start() command:

extPro.StartInfo.WorkingDirectory = @"E:\gnuplot\bin";

or

Environment.CurrentDirectory = @"E:\gnuplot\bin";

If this is not working, it might be because your application does not have read access to that directory. Try placing your insert.dat file in a directory that with certainty can be accessed by any client application.

BTW, I also do not recognize the Standardization property that you are using? That line should rather read:

extPro.StartInfo.RedirectStandardInput = true;
like image 63
Anders Gustafsson Avatar answered Sep 26 '26 22:09

Anders Gustafsson