Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
457 views
in Technique[技术] by (71.8m points)

awk - Plotting a function directly from a text file

Is there a way to plot a function based on values from a text file?

I know how to define a function in gnuplot and then plot it but that is not what I need. I have a table with constants for functions that are updated regularly. When this update happens I want to be able to run a script that draws a figure with this new curve. Since there are quite few figures to draw I want to automate the procedure.

Here is an example table with constants:

location a  b  c
1        1  3  4
2

There are two ways I see to solve the problem but I do not know if and how they can be implemented.

  1. I can then use awk to produce the string: f(x)=1(x)**2+3(x)+4, write it to a file and somehow make gnuplot read this new file and plot on a certain x range.
  2. or use awk inside gnuplot something like f(x) = awk /1/ {print "f(x)="$2 etc., or use awk directly in the plot command.

I any case, I'm stuck and have not found a solution to this problem online, do you have any suggestions?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Another possibilty to have a somewhat generic version for this, you can do the following:

Assume, the parameters are stored in a file parameters.dat with the first line containing the variable names and all others the parameter sets, like

location a b c
1        1 3 4

The script file looks like this:

file = 'parameters.dat'
par_names = system('head -1 '.file)
par_cnt = words(par_names)

# which parameter set to choose
par_line_num = 2
# select the respective string
par_line = system(sprintf('head -%d ', par_line_num).file.' | tail -1')
par_string = ''
do for [i=1:par_cnt] {
  eval(word(par_names, i).' = '.word(par_line, i))
}
f(x) = a*x**2 + b*x + c

plot f(x) title sprintf('location = %d', location)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...