gnuplot 将数据文件中的一个数字存储到变量中 [英] gnuplot store one number from data file into variable

查看:19
本文介绍了gnuplot 将数据文件中的一个数字存储到变量中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

OSX v10.6.8 和 Gnuplot v4.4

OSX v10.6.8 and Gnuplot v4.4

我有一个包含 8 列的数据文件.我想从第 6 列中获取第一个值并将其作为标题.这是我到目前为止所拥有的:

I have a data file with 8 columns. I would like to take the first value from the 6th column and make it the title. Here's what I have so far:

#m1 m2 q taua taue K avgPeriodRatio time
#1  2  3   4   5   6        7        8

K = #read in data here
graph(n) = sprintf("K=%.2e",n) 
set term aqua enhanced font "Times-Roman,18"

plot file using 1:3 title graph(K)

这是我的数据文件的前几行:

And here is what the first few rows of my data file looks like:

1.00e-07 1.00e-07 1.00e+00 1.00e+05 1.00e+04 1.00e+01 1.310 12070.00
1.11e-06 1.00e-07 9.02e-02 1.00e+05 1.00e+04 1.00e+01 1.310 12070.00
2.12e-06 1.00e-07 4.72e-02 1.00e+05 1.00e+04 1.00e+01 1.310 12070.00
3.13e-06 1.00e-07 3.20e-02 1.00e+05 1.00e+04 1.00e+01 1.310 12090.00

我不知道如何正确读取数据,也不知道这是否是解决此问题的正确方法.

I don't know how to correctly read in the data or if this is even the right way to go about this.

编辑 #1

好的,感谢 mgilson,我现在有了

Ok, thanks to mgilson I now have

#m1 m2 q taua taue K avgPeriodRatio time
#1  2  3   4   5   6        7        8

set term aqua enhanced font "Times-Roman,18"
K = "`head -1 datafile | awk '{print $6}'`"
print K+0
graph(n) = sprintf("K=%.2e",n) 

plot file using 1:3 title graph(K)

但我收到错误:在需要数字表达式的地方找到了非数字字符串

but I get the error: Non-numeric string found where a numeric expression was expected

编辑#2

file = "testPlot.txt"
K = "`head -1 file | awk '{print $6}'`"
K=K+0  #Cast K to a floating point number  #this is line 9
graph(n) = sprintf("K=%.2e",n)
plot file using 1:3 title graph(K)

这给出了错误--> head: file: No such file or directorytestPlot.gnu",第 9 行:在需要数字表达式的地方找到非数字字符串

This gives the error--> head: file: No such file or directory "testPlot.gnu", line 9: Non-numeric string found where a numeric expression was expected

推荐答案

您有几个选择...

第一个选项:

使用columnheader

plot file using 1:3 title columnheader(6)

我还没有测试过,但这可能会阻止实际绘制第一行.

I haven't tested it, but this may prevent the first row from actually being plotted.

第二个选项:

使用外部实用程序获取标题:

use an external utility to get the title:

TITLE="`head -1 datafile | awk '{print $6}'`"
plot 'datafile' using 1:3 title TITLE

如果变量是数字,并且您想重新格式化它,在 gnuplot 中,您可以通过向字符串添加 0(例如)将字符串转换为数字类型(整数/浮点数).

If the variable is numeric, and you want to reformat it, in gnuplot, you can cast strings to a numeric type (integer/float) by adding 0 to them (e.g).

print "36.5"+0

然后你可以像你已经在做的那样使用 sprintfgprintf 对其进行格式化.

Then you can format it with sprintf or gprintf as you're already doing.

奇怪的是没有 float 函数.(int 如果你想转换为整数就可以了).

It's weird that there is no float function. (int will work if you want to cast to an integer).

编辑

下面的脚本对我有用(当我将您的示例数据粘贴到名为datafile"的文件中时):

The script below worked for me (when I pasted your example data into a file called "datafile"):

K = "`head -1 datafile | awk '{print $6}'`"
K=K+0  #Cast K to a floating point number
graph(n) = sprintf("K=%.2e",n)
plot "datafile" using 1:3 title graph(K)

EDIT 2(解决下面的评论)

要在 backtics 中展开变量,您需要宏:

To expand a variable in backtics, you'll need macros:

set macro
file="mydatafile.txt"
#THE ORDER OF QUOTES (' and ") IS CRUCIAL HERE.
cmd='"`head -1 ' . file . ' | awk ''{print $6}''`"'
# . is string concatenation.  (this string has 3 pieces)
# to get a single quote inside a single quoted string
#   you need to double.  e.g. 'a''b' yields the string a'b 
data=@cmd

为了解决您的问题 2,最好熟悉 shell 实用程序——sed 和 awk 都可以做到.我将展示头/尾的组合:

To address your question 2, it is a good idea to familiarize yourself with shell utilities -- sed and awk can both do it. I'll show a combination of head/tail:

cmd='"`head -2 ' . file . ' | tail -1 | awk ''{print $6}''`"'

应该可以.

编辑 3

我最近了解到在 gnuplot 中,system 是一个函数,也是一个命令.要在没有所有背部体操的情况下完成上述操作,

I recently learned that in gnuplot, system is a function as well as a command. To do the above without all the backtic gymnastics,

data=system("head -1 " . file . " | awk '{print $6}'")

哇,好多了.

这篇关于gnuplot 将数据文件中的一个数字存储到变量中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆