Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将数据集值读入gnuplot变量(X系列的开始)_Gnuplot - Fatal编程技术网

将数据集值读入gnuplot变量(X系列的开始)

将数据集值读入gnuplot变量(X系列的开始),gnuplot,Gnuplot,我最初认为这可能与-相同,但我认为这稍微更具体一些 既然我对寻找“X系列的开始”感兴趣,可以这么说——我将尝试用一个例子来澄清;假设您有以下脚本: # generate data system "cat > ./inline.dat <<EOF\n\ 10.0 1 a 2\n\ 10.2 2 b 2\n\ 10.4 3 a 2\n\ 10.6 4 b 2\n\ 10.8 5 c 7\n\ 11.0 5 c 7\n\ EOF\n" # ranges set yrange [0

我最初认为这可能与-相同,但我认为这稍微更具体一些

既然我对寻找“X系列的开始”感兴趣,可以这么说——我将尝试用一个例子来澄清;假设您有以下脚本:

# generate data
system "cat > ./inline.dat <<EOF\n\
10.0 1 a 2\n\
10.2 2 b 2\n\
10.4 3 a 2\n\
10.6 4 b 2\n\
10.8 5 c 7\n\
11.0 5 c 7\n\
EOF\n"

# ranges 
set yrange [0:8]
set xrange [0:11.5]

plot "inline.dat" using 1:2 with impulses linewidth 2
。。。基本上就是这样

但是假设您不想在上面的
绘图
命令中明确指定“10.0”;然后——知道它是已经加载的数据的第一列的第一个元素,我们希望有一种方法能够以某种方式读取变量中的该值——比如,使用以下伪代码:

varval = "inline.dat"(1,1) # get first element of first column in variable
plot "inline.dat" using ($1-varval):2 with impulses linewidth 2
。。。有了这样的东西,就不必在plot命令中手动指定这个“x偏移”值了


那么,换言之,有没有一种方法可以将x系列的开头(数据集中给定列的第一个值)作为变量读取到
gnuplot

Hmmm。。。好的,我得到了一些东西:

initer(x) = (!exists("first")) ? first = x : first ;
plot "inline.dat" using ($1-initer($1)):2 with impulses linewidth 2
。。。但这看起来更像是“捕获”一个变量,而不是读取它(因为函数
initer
被用来扫描一个数字流,检测第一个数字,并返回其值):)希望有更好的方法做到这一点

两种方式:

一,。 首先绘制函数,让gnuplot告诉最小x值:

plot "inline.dat" using 1:2 with impulses linewidth 2

xmin = GPVAL_DATA_X_MIN
plot "inline.dat" using ($1-xmin):2 with impulses linewidth 2
xmin = `sort -nk 1 inline.dat | head -n 1 | awk '{print $1}'`
plot "inline.dat" using ($1-xmin):2 with impulses linewidth 2
二,。 使用外部脚本计算最小x值:

plot "inline.dat" using 1:2 with impulses linewidth 2

xmin = GPVAL_DATA_X_MIN
plot "inline.dat" using ($1-xmin):2 with impulses linewidth 2
xmin = `sort -nk 1 inline.dat | head -n 1 | awk '{print $1}'`
plot "inline.dat" using ($1-xmin):2 with impulses linewidth 2

好的,我继续回到这里-因此我认为我需要以下澄清:

考虑到
gnuplot
,那么,将数据集绘制为2D绘图-它以某种方式处理2D结构或数组。这就是为什么来自C、Perl、Python等的人自然会认为可以以某种方式索引数据集,并且能够在给定的行和列位置检索值;比如说,类似于以下伪代码:

my_val = "inline.dat"[1][2]     # get value in 1st row and 2nd column
或者,伪代码:
my\u dataset=parse\u数据集(“inline.dat”)

my_val=get_值(my_数据集,1,2)

我花了很多时间在
gnuplot
中查找类似的内容,但找不到类似的内容(通过行和列索引直接访问数据集值的变量)。似乎唯一能做的事情是
绘制
数据集,并可能通过
使用
部分中调用的函数访问数据集中的值

这意味着,如果我想从
gnuplot
中找到一些数据集值,我必须通过调用
plot
来迭代数据集-即使我需要这些值来精确地构造一个适当的
plot
语句
:)
,我有点不喜欢这样,认为第一个
绘图
可能会在某种程度上搞砸后面的第二个
:)
然而,正如所指出的,人们可以
绘图
到一个文件,也可以
标准输出
/dev/null
,并得到一个简单的ASCII格式的表-因此至少我可以用那种方式重定向第一个调用,因此,它不会干扰第二次调用
plot
的实际绘图终端

下面是另一个代码示例,其中“
inline.dat
”数据集中第一列的第一个元素通过以下方式检索:

# print and get (in _drcv) first value of first data column:
eval print_dataset_row_column("inline.dat",0,1)

# must "copy" the "return" value manually:
first = _drcv
。。。因此,可以直接在
plot
调用中通过
first
对绘图进行偏移

再次注意,
print\u dataset\u row\u column
调用
plot
(通过
set table
重定向到
/dev/null
)——因此,每次调用它来检索单个值时,它都会导致整个数据集的迭代!因此,如果您需要第一个元素和最后一个元素(可能还有其他东西,比如),那么最好重写
print\u dataset\u row\u column
,以便它一次检索所有这些元素

此外,如果在数据集中使用某些特殊格式,并且使用
行,则需要重写
print\u dataset\u row\u column
。请注意,在本例中,第三列是一个字符串,默认情况下不接受该字符串作为绘图数据列;因此,如果必须处理
print\u dataset.*
函数,则调用这些函数将失败(另请参见)

下面是示例代码-我们称之为
test.gp

# generate data
system "cat > ./inline.dat <<EOF\n\
10.0 1 a 2\n\
10.2 2 b 2\n\
10.4 3 a 2\n\
10.6 4 b 2\n\
10.8 5 c 7\n\
11.0 5 c 7\n\
EOF\n"

### "dry-run" functions:
# iterate through dataset by calling
# `plot`, redirected to file output (via `set table`)
#
# note: eval (not print) cannot be inside a user-defined function:
#  a(b) = eval('c=3') ; print a(4) ==> "undefined function: eval"
# nor you can make multistatement functions with semicolon:
#  f(x) = (2*x ; x=x+2) ==> ')' expected (at ';')
#
# so these functions are defined as strings - and called through eval
#
# through a single column spec in `using`:
# (`plot` prints table to stdout)
#
print_dataset_column(filename,col) = "set table '/dev/stdout' ;\
plot '".filename."' using ".col." ;\
unset table"
#
# through two column spec in `using`:
# (`plot` prints table to stdout)
#
print_dataset_twocolumn(filename,colA,colB) = "set table '/dev/stdout' ;\
plot '".filename."' using ".colA.":".colB." ;\
unset table"
#
# print value of row:column in dataset, saving it as _drcv variable
#
# init variable
#
_drcv = 0
#
# create _drc helper function; note assign and "return" in
# true branch of ternary clause
#
_drc(ri, colval, col) = (ri == _row) ? _drcv = colval : colval
#
# define the callable function:
#
print_dataset_row_column(filename,row,col) = "_row = ".row." ;\
set table '/dev/null' ;\
plot '".filename."' using (_drc($0, $".col.", ".col.")) ;\
unset table ;\
print '".filename."[r:".row.",c:".col."] = ',_drcv"
#
#
### end dry run functions


#
# test print_dataset_* functions:
#

eval print_dataset_column("inline.dat",0)
eval print_dataset_twocolumn("inline.dat",0,0)

# string column - cannot directly:
# set table '/dev/stdout' ;plot 'inline.dat' using 3 ;unset table
#                                                  ^
# line 69: warning: Skipping data file with no valid points
# line 69: x range is invalid
#~ eval print_dataset_column("inline.dat",3)

eval print_dataset_column("inline.dat",1)
eval print_dataset_twocolumn("inline.dat",1,2)

eval print_dataset_row_column("inline.dat",4,1)
eval print_dataset_row_column("inline.dat",4,2)

# will fail - 3 is string column
# line 82: warning: Skipping data file with no valid points
# line 82: x range is invalid
#~ eval print_dataset_row_column("inline.dat",4,3)


#
# do a plot offset by first element position
#

# print and get (in _drcv) first value of first data column:
eval print_dataset_row_column("inline.dat",0,1)
# must "copy" the "return" value manually:
first = _drcv

# ranges
set yrange [0:8]
set xrange [0:11.5]

# plot finally:
plot "inline.dat" using ($1-first):2 with impulses linewidth 2

< >从数据文件中读取单个值,请考虑下面的用户定义函数:

at(file, row, col) = system( sprintf("awk -v row=%d -v col=%d 'NR == row {print $col}' %s", row, col, file) )
file="delta-fps" ; row=2 ; col=2
print at(file,row,col)
当然,必须清除awk的输入中被忽略/无效的输入(空行、注释等)。例如:

at(file, row, col) = system( sprintf("grep -v '^#|^$' %s | awk -v row=%d -v col=%d 'NR == row {print $col}'", file, row, col) )

不过,此函数不允许读取任何数据集,它仅限于文件。然而,这个限制可以通过检查重定向角色'Awesome'来克服——非常感谢您的简洁回答,@Sunhwan Jo-干杯!是的,但是,如果您对数据系列的第一个元素感兴趣,它不一定需要是最小值,那该怎么办?@TMOTTM然后您必须调整外部脚本以仅使用第一个元素(直接使用awk而不使用排序部分)。谢谢您,@Hannes-请记住handy方法。干杯
at(file, row, col)=system( sprintf("%s | grep -v '^#\\|^$' | awk -v row=%d -v col=%d 'NR == row {print $col}'", (file[:1] eq '<') ? file[2:] :'cat '.file, row, col) )