Question

I want to draw a function with gnuplot and I need to read the slope of the line from a .txt file. How can I look for this variable with gnuplot?

this is the case:

My .txt file contains:

0.0121200419333 / 200497.710163

0.0150797824833 / 200496.896578

and I need to read the data from the column 1 row 2 and set it as the slope of f(x)=a*x+b and draw it

Was it helpful?

Solution

I think (please be more specific [updated at the end of the answer] if you want more specific help) you have a file (call it file for the sake of being original) like this:

This is a file with some text in it.
The slope of my curve should be slope = 6.
And some more text here.

And want to use the value slope = 6 as the slope of a function in gnuplot. You need to read in this number, for which you should use some bash utility once you know how to find it. In the very simple case above, you could look for slope = in your file with grep and then use = as a delimiter with cut and tell awk to print the first thing it finds:

grep "slope =" file | cut -d "=" -f2 | awk '{print $1}'

Now go to gnuplot and use this as a variable:

slope = `grep "slope =" file | cut -d "=" -f2 | awk '{print $1}'`
plot slope*x

And this is what you get in my example, where slope = 6:

enter image description here

You'll probably need to adapt this to your problem but unless you include more details it's impossible to give you better guidance.

For the specific case where you want the first record on the second line:

a = `awk 'NR==2 {print $1}' file`
plot a*x
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top