英文:
How to paste gnuplot datablocks?
问题
我必须在gnuplot的for循环中计算新值。我怎样才能粘贴$Data的结果?我收到错误消息“数据块名称后必须跟随<<EODmarker”。
do for [selected_label in LABELS] {
set table $Data
plot '' index group using ( @sel ):( @corrected_volume ) with table
unset table
$Temp = $Temp . $Data
}
print $Temp
英文:
I have to calculate new values within a for loop in gnuplot. How can I paste the results from $Data? I am getting the error message "data block name must be followed by << EODmarker".
do for [selected_label in LABELS] {
set table $Data
plot '' index group using ( @sel ):( @corrected_volume ) with table
unset table
$Temp = $Temp . $Data
}
print $Temp
答案1
得分: 2
Here's the translated content:
检查`help table`。您可以附加到数据块。
我不知道您的脚本的详细信息和依赖关系,但请查看下面的示例。希望它有所帮助。
顺便说一下,您也可以使用
set print $Temp append
print "something"
set print
**脚本:**
```plaintext
### 追加到现有数据块
reset session
$Temp <<EOD
1 4
2 5
3 6
EOD
set table $Temp append
do for [i=1:3] {
set samples 3
set xrange [0:2]
plot '+' u ($1*i):(($1*i)**2) w table
}
unset table
print $Temp
### 脚本结束
结果:
1 4
2 5
3 6
0 0
1 1
2 4
0 0
2 4
4 16
0 0
3 9
6 36
附加信息:
您可以在gnuplot中模仿Linux的paste
命令。您可以索引数据块的行(索引从1开始),删除最后一个字符(即换行符\n
),然后与另一个字符串连接并打印到新的数据块。您需要确保不要添加比原始数据块更多的行。
脚本:(需要gnuplot >= 5.2.0,因为涉及到数据块的索引)
### 仅使用gnuplot模仿Linux的paste
reset session
$Temp <<EOD
1 4
2 5
3 6
EOD
set print $Temp2
do for [j=0:2] {
tmp_line = ''
do for [i=1:3] {
tmp_line = tmp_line.sprintf(" %g %g", j*i, (j*i)**2)
}
print $Temp[j+1][1:strlen($Temp[j+1])-1].tmp_line
}
set print
print $Temp2
### 脚本结束
结果:
1 4 0 0 0 0 0 0
2 5 1 1 2 4 3 9
3 6 2 4 4 16 6 36
附加信息 2:
以下内容模仿了Linux的“paste”命令,其中在循环中使用了plot ... w table
。这变得有点复杂,并需要第三个数据块$Temp3
。一旦将$Temp2
附加到$Temp
中的$Temp3
,您会覆盖$Temp
并开始下一个迭代。根据您详细的脚本,这可能可以简化。
对于换行字符,有一些特殊情况,我不完全理解。因此,显然您必须跳过数据块行的最后一个字符,但仅限于第一次(i==1
)。此外,在写入表时,强制数据文件分隔符为空格,否则它将为TAB
。混合的列分隔符不好看,但只要您有set datafile separator whitespace
(这是默认设置),gnuplot也会处理这个问题。
脚本:(需要gnuplot >= 5.2.0,因为涉及到数据块的索引)
### 仅使用gnuplot模仿Linux的paste
reset session
$Temp <<EOD
1 4
2 5
3 6
EOD
do for [i=1:3] {
set table $Temp2 separator " "
set samples 3
set xrange [0:2]
plot '+' u ($1*i):(($1*i)**2) w table
unset table
set print $Temp3
do for [j=1:|$Temp2|] {
print $Temp[j][1:strlen($Temp[j])-(i==1)].$Temp2[j][1:strlen($Temp2[j])]
}
set print $Temp
print $Temp3
set print
}
print $Temp
### 脚本结束
结果:
1 4 0 0 0 0 0 0
2 5 1 1 2 4 3 9
3 6 2 4 4 16 6 36
英文:
Check help table
. You can append to a datablock.
I don't know the details and the dependencies of your script, but check the example below. Hope it helps.
By the way, you could also use
set print $Temp append
print "something"
set print
Script:
### append to an existing datablock
reset session
$Temp <<EOD
1 4
2 5
3 6
EOD
set table $Temp append
do for [i=1:3] {
set samples 3
set xrange [0:2]
plot '+' u ($1*i):(($1*i)**2) w table
}
unset table
print $Temp
### end of script
Result:
1 4
2 5
3 6
0 0
1 1
2 4
0 0
2 4
4 16
0 0
3 9
6 36
Addition:
You can mimic Linux' paste command in gnuplot-only. You index the lines of the datablock (index is 1-based) and remove the last character (which is newline \n
) and concatenate it with another string and print it to a new datablock. You need to make sure you add not more lines than the original datablock has.
Script: (requires gnuplot>=5.2.0, because of indexing of datablocks)
### mimic Linux' paste with gnuplot only
reset session
$Temp <<EOD
1 4
2 5
3 6
EOD
set print $Temp2
do for [j=0:2] {
tmp_line = ''
do for [i=1:3] {
tmp_line = tmp_line.sprintf(" %g %g", j*i, (j*i)**2)
}
print $Temp[j+1][1:strlen($Temp[j+1])-1].tmp_line
}
set print
print $Temp2
### end of script
Result:
1 4 0 0 0 0 0 0
2 5 1 1 2 4 3 9
3 6 2 4 4 16 6 36
Addition 2:
The following is mimicing Linux' "paste" having a plot ... w table
in the loop. This get's a bit ugly and requires a third datablock $Temp3
.
Once you appended $Temp2
to $Temp
in $Temp3
you overwrite $Temp
with $Temp3
and start over for the next iteration. Based on your detailed script this could maybe be simplified.
There are some special things ongoing with the newline character, which I don't fully understand. So, apparently you have to skip the last character of the datablock line, but only for the first time (i==1
). Furthermore, force the datafile separator to be space when writing to the table, otherwise it would be TAB
. Mixed column separators are not nice, but gnuplot would also handle this as long as you have set datafile separator whitespace
which is the default setting.
Script: (requires gnuplot>=5.2.0, because of indexing of datablocks)
### mimic Linux' paste with gnuplot only
reset session
$Temp <<EOD
1 4
2 5
3 6
EOD
do for [i=1:3] {
set table $Temp2 separator " "
set samples 3
set xrange [0:2]
plot '+' u ($1*i):(($1*i)**2) w table
unset table
set print $Temp3
do for [j=1:|$Temp2|] {
print $Temp[j][1:strlen($Temp[j])-(i==1)].$Temp2[j][1:strlen($Temp2[j])]
}
set print $Temp
print $Temp3
set print
}
print $Temp
### end of script
Result:
1 4 0 0 0 0 0 0
2 5 1 1 2 4 3 9
3 6 2 4 4 16 6 36
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论