1

In Gnuplot, I try to assign a value to a function f(x) from a two column data file in order to plot that function as a horizontal line.

f(x)=value of $2 at $1==2 from filename.dat
plot 'filename.dat' , ' ' x<=2?f(x):1/0

I tried :

awk '$1==2,print{$2}' filename.dat

but it says :

Warning: encountered a string when expecting a number Did you try to generate a file name using dummy variable x or y?".

Any suggestions?

P.S.: I know that there should be a < sign after awk, but it would not display it here.

divibisan
  • 11,659
  • 11
  • 40
  • 58
Paul Rousseau
  • 571
  • 2
  • 7
  • 21

1 Answers1

0

I suggest this approach:

filename = "test.txt"
y = system(sprintf("awk '$1==2{print $2}' %s", filename))
f(x)=real(y)
plot filename u 1:2 w l, f(x) t ''

It seems that gnuplot is interpreting the value of y as a string. Therefore, we have to enforce a conversion to a number, which I do by converting it to a real number. (Another possibility f(x)=y+0)

Here is the ouput for my demo test.txt with the following content:

1 1
2 0.5
3 0

enter image description here

F. Knorr
  • 3,045
  • 15
  • 22
  • Thank you so much, I did not think it would be that easy, but every time you search for "basic" stuff like this, it is always stumbling upon very advanced stuff. Do you know a place where I can get more basic information about awk like this one ?? – Paul Rousseau Nov 28 '15 at 17:49
  • @PaulRousseau: Glad that I could help. If you are satisfied with the answer, please mark the question answered. For more information an awk see [Awk by example](http://www.ibm.com/developerworks/library/l-awk1/) or the [Awk Introduction Tutorial](http://www.thegeekstuff.com/2010/01/awk-introduction-tutorial-7-awk-print-examples/) – F. Knorr Nov 28 '15 at 18:16