首页 > 解决方案 > Gnuplot:如何从数据文件中读取一个文本字符串?

问题描述

我正忙于从一个大的 csv 格式数据文件生成一些图的系统。

到目前为止,我可以用 gnuplot 完成这项任务,但我也喜欢从数据文件中读取一些文本字符串,进行一些调整并将它们用于标记和/或图表上的其他文本框。到目前为止,我无法在 gnuplot 命令系统中执行此操作。

额外要求:应该能够在 Windows 上运行代码。

到目前为止,这是我的代码:

# The BigDataFile.csv files starts with some lines information
# (string texts about the matrix contense). After this info block,
# all the matrix data for  the plots.

dataFileName="BigDataFile.csv"

set datafile separator "\t"     

# Read one text string at line number 'textInfoRow' from column
# number 'textInfoColumn'

textInfoRow = 5

textInfoColumn = 10

# Now try to capture the string at some location in the info block. 

set terminal unknown 

plot dataFileName every ::textRow::textRow using (textVar=stringcolumn(textColumn))  # How can we do this ????

print textVar 

问题:我们如何在“gnuplot”中读取数据文件中指定位置的单个文本字符串?

标签: gnuplot

解决方案


您的代码的问题是:

  1. 你的变量是textInfoRowandtextInfoColumn和之后的textRowand textColumn
  2. gnuplot 不喜欢绘制字符串,即使终端设置为unknown. 只需将您的表达式更改为(textVar=stringcolumn(textInfoColumn),0). 它正在为变量赋值,但正在绘图0。检查help operators binary。表达式(a,b)是串行评估。

还要记住,gnuplot 从0.

数据:( "BigDataFile.csv"确保分隔符是 TAB,StackOverflow 将它们转换为 SPACE)

# "Big" data with some header
# another header line
1.000   2.000   3.000   4.000   5.000   6.000   7.000   8.000   9.000   10.00
2.000   0.982   0.755   0.526   0.684   0.090   0.221   0.402   0.594   0.331
3.000   0.119   0.904   0.938   0.960   0.067   0.607   0.368   0.540   0.317
4.000   0.782   0.060   0.163   0.446   0.826   0.503   0.096   0.494   0.949
5.000   0.930   0.703   0.294   0.990   0.919   0.038   0.550   0.467   HERE
6.000   0.346   0.830   0.920   0.285   0.575   0.878   0.747   0.532   0.222
7.000   0.207   0.120   0.709   0.194   0.854   0.501   0.241   0.505   0.123
8.000   0.862   0.479   0.531   0.640   0.259   0.673   0.708   0.559   0.516
9.000   0.979   0.581   0.611   0.664   0.369   0.775   0.808   0.522   0.294
10.00   0.514   0.516   0.780   0.232   0.407   0.718   0.140   0.568   0.619

您可以通过以下代码提取文本“单元格”。我不确定这是否是最快和最有效的方法,但至少它是仅 gnuplot 的,因此与平台无关,应该在 Linux、MacOS 和 Windows 下顺利运行。

代码:

### extract one "cell" of data
reset session

dataFileName="BigDataFile.csv"

set datafile separator "\t"     
textInfoRow = 5
textInfoColumn = 10

set terminal unknown 
plot dataFileName every ::textInfoRow-1::textInfoRow-1 using (textVar=stringcolumn(textInfoColumn),0) 

print textVar 
### end of code

结果:

HERE


推荐阅读