如何使用gnuplot导入具有固定列宽的数据?

问题描述 投票:0回答:1

gnuplot是否有一种简单的方法来导入具有固定宽度的列的数据?

问题是列可能为空,因此使用set datafile separator whitespace导入将无法正常进行。我知道您可以使用awk,sed等外部工具对数据进行预处理,但我想知道是否可能有一个独立于平台的简单gnuplot-only解决方案。

我想出的解决方案有点冗长,但至少看来可行。如果有更简单的gnuplot方式,请让我知道。

代码:

### data with fixed column widths
reset session

$DataRaw <<EOD
# Data with fixed but also empty columns
#0000000011111111112222222222333333333344444
#2345678901234567890123456789012345678901234
#--6-||----11---||--7--||-----12---||--8---|
   1.1     1.2222   1.03   some Text   1.555
   2.1    -2.2222                     -2.555
  -3.1     3.2222  -3.03   more text   3.555
   4.1             -4.03              -4.555
                             no data   0.000
   6.1    -6.2222   6.03  no comment  -6.555
EOD

# define the widths of the columns
array FixCols[5] = [6,11,7,12,8]
set datafile separator "\n"
CommentChar = "#"
Separator = ','

# define strip() function workaround to remove spaces at beginning and end of a string
strip(s) = (STRP_a=1, STRP_b=1, \
           sum [STRP_i=1:strlen(s)] ((s[STRP_i:STRP_i] eq " ") ? \
               (STRP_a>0  ? STRP_a=STRP_a+1 : 0) : (STRP_a=-abs(STRP_a), STRP_b=STRP_i) \
           ), s[abs(STRP_a):STRP_b] )

set print $Data
    do for [i=1:|$DataRaw|] {
        if ($DataRaw[i][1:1] ne CommentChar) {
            Line = ''
            Start = 1
            do for [j=1:|FixCols|] {
                End = Start + FixCols[j]-1
                Line = Line.strip($DataRaw[i][Start:End]).(j<|FixCols| ? Separator : "")
                Start = Start + FixCols[j]
            }
            print Line
        }
        else { print $DataRaw[i]}  # print the unchanged commented line
    }
set print

print $Data
### end of code

结果:

# Data with fixed but also empty columns
#0000000011111111112222222222333333333344444
#2345678901234567890123456789012345678901234
#--6-||----11---||--7--||-----12---||--8---|
1.1,1.2222,1.03,some Text,1.555
2.1,-2.2222,,,-2.555
-3.1,3.2222,-3.03,more text,3.555
4.1,,-4.03,,-4.555
,,,no data,0.000
6.1,-6.2222,6.03,no comment,-6.555
gnuplot
1个回答
0
投票

gnuplot始终可以选择遵循using规范指定格式,但是该实现早于引入字符串变量。因此,您可以从具有固定宽度字段的行中读取数字,但是我无法立即看到如何在同一命令中以字符串形式读取字段内容。输入扫描使用C语言例程sscanf()。数字始终需要格式规范%lf。要跳过N个字符字段,请使用格式规范%*Nc

$DataRaw <<EOD
# Data with fixed but also empty columns
#0000000011111111112222222222333333333344444
#2345678901234567890123456789012345678901234
#--6-||----11---||--7--||-----12---||--8---|
   1.1     1.2222   1.03   some Text   1.555
   2.1    -2.2222                     -2.555
  -3.1     3.2222  -3.03   more text   3.555
   4.1             -4.03              -4.555
                             no data   0.000
   6.1    -6.2222   6.03  no comment  -6.555
EOD

set table 
splot $DataRaw skip 4 using 1:2:3:(sprintf("%g",$4)) "%6lf%11lf%7lf%*12c%8lf" with labels

产生

# Surface 0 of 1 surfaces

# Curve title: "$DataRaw skip 4 using 1:2:3:(sprintf("%g",$4)) "%6lf%11lf%7lf%*12c%8lf""
 1.1  1.2222  1.03 "1.555"
-3.1  3.2222 -3.03 "3.555"
 6.1 -6.2222  6.03 "-6.555"
© www.soinside.com 2019 - 2024. All rights reserved.