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.

1

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

2 Comments

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 ??
@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 or the Awk Introduction Tutorial

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.