直接从文本文件绘图功能 [英] Plotting a function directly from a text file
问题描述
有没有一种方法来绘制基于价值观从一个文本文件中的函数?
Is there a way to plot a function based on values from a text file?
我知道如何定义gnuplot的一个函数,然后绘制它,但是这不是我所需要的。
我有与该定期更新功能常数表。当这种情况发生更新我希望能够运行,吸引了这种新的曲线的图形脚本。由于有相当一些数字得出我要自动运行的程序。
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.
下面是常量的示例表:
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.
- 然后我可以使用awk来生成字符串:
F(X)= 1(X)** 2 + 3(X)+4
,将其写入文件,并以某种方式使gnuplot的阅读一定X
系列。 这个新文件和剧情 - 或使用的gnuplot里面的东西AWK像
F(X)= AWK / 1 / {打印F(X)=$ 2
等,或AWK直接使用plot命令。
- 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 certainx
range. - 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?
推荐答案
另外possibilty有这几分通用版本,你可以做到以下几点:
Another possibilty to have a somewhat generic version for this, you can do the following:
假设,所述参数被存储在一个文件中 parameters.dat
与含有变量名的第一行和所有其他的参数集,如
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)
这篇关于直接从文本文件绘图功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!