Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.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导入具有固定列宽的数据?_Gnuplot - Fatal编程技术网

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

如何使用gnuplot导入具有固定列宽的数据?,gnuplot,Gnuplot,gnuplot是否有一种简单的方法可以导入具有固定列宽的数据 问题是列可能为空,因此使用set datafile separator whitespace导入将无法正常工作。 我知道您可以使用外部工具(如awk、sed等)预处理数据,但我想知道是否有一个简单的独立于平台的gnuplot解决方案 我提出的解决方案有点冗长,但至少它似乎有效。如果有更简单的gnuplot方法,请让我知道 代码: ### data with fixed column widths reset session $Dat

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

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

我提出的解决方案有点冗长,但至少它似乎有效。如果有更简单的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始终可以选择使用规范指定
之后的格式,但是实现要早于字符串变量的引入。因此,您可以从具有固定宽度字段的行中读取数字,但我无法立即看到如何在同一命令中以字符串形式读取字段内容。输入扫描使用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

非常感谢。我不知道这件事。不幸的是,它没有帮助,因为它无法读取文本,如果有空列而不是插入,它将跳过整行,例如空格或
NaN
# 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"