2

我得到了一些包含相同测量结果的表格文件,但由于差异很小,数据点得到了不同的xy值。但我需要所有六个文件的平均值。以下是两个(缩短的)示例文件:

# file1.dat
  9.840000000000000E+00  1.680000000000000E+02
  9.840071206052514E+00  1.730000000000000E+02
  9.840142412105029E+00  1.630000000000000E+02
  9.840213618157543E+00  1.730000000000000E+02
  9.840284824210057E+00  1.690000000000000E+02
  9.840356030262573E+00  1.720000000000000E+02
  9.840427236315087E+00  1.660000000000000E+02
  9.840498442367601E+00  1.750000000000000E+02
  9.840569648420116E+00  1.650000000000000E+02
  9.840640854472630E+00  1.720000000000000E+02

# file2.dat
  9.840000000000000E+00  1.720000000000000E+02
  9.840071016422547E+00  1.760000000000000E+02
  9.840142032845096E+00  1.610000000000000E+02
  9.840213049267643E+00  1.530000000000000E+02
  9.840284065690192E+00  1.590000000000000E+02
  9.840355082112739E+00  1.590000000000000E+02
  9.840426098535286E+00  1.690000000000000E+02
  9.840497114957834E+00  1.790000000000000E+02
  9.840568131380381E+00  1.680000000000000E+02
  9.840639147802928E+00  1.620000000000000E+02

[还有四个类似的文件……]

如果我将它们全部绘制成gnuplot除了每个表作为自己的曲线,我会得到这个
gnuplot

但我需要一条曲线来显示所有六张表的平均值。

我试过join了,但结果是一个空文件,我猜它不起作用,因为第一列(x值)在每个文件中不包含相同的值。尝试用gnuplotwith进行求和plot data1 u 1:2 + data2 u 1:2也失败了。

我发现合并多个数据文件以绘制在一个图表中,但仅合并文件并没有帮助。


我使用的是 Mac OS X,但可以访问 Ubuntu,所以如果有任何工具可以做到这一点……</p>


脚本的新输出

> AVDEBUG=1 octave -qf avfiles.m messung3.dat messung4.dat
warning: ================
warning: processing file: messung3.dat
warning: size of the matrix in the file: 2248      3
warning: min(x): 0.000000e+00
warning: max(x): 0.000000e+00
warning: min(y): 9.840000e+00
warning: max(y): 1.000000e+01
warning: ================
warning: processing file: messung4.dat
warning: size of the matrix in the file: 2254      3
warning: min(x): 0.000000e+00
warning: max(x): 0.000000e+00
warning: min(y): 9.840000e+00
warning: max(y): 1.000000e+01
warning: yp is undefined at 2248 points

如果您愿意,可以从这里 (tweh.de/texsx/data-files-avarage.zip)下载我的数据文件。平均应由所有文件组成。

4

1 回答 1

1

八度音阶很容易。创建文件avfiles.m. 用法:

octave -qf avfiles.m file1.dat file2.dat

octave -q --eval "test avfiles"使用测试数据创建文件aux_file[0-9]

avfiles.m:

#!/usr/bin/octave -qf
# Average y values in several files
# Usage:
# octave -qf avfiles.m aux_file0 aux_file1 aux_ file2

if isempty(getenv("AVDEBUG"))
  warning ("off", "avfiles")
endif

setenv("LC_NUMERIC", "C"); 

arg_list = argv ();
for k = 1:nargin
  fname = arg_list{k};
    warning ("avfiles", "================");
    warning ("avfiles", "processing file: %s", fname);
    data = dlmread(fname);
    warning ("avfiles", "size of the matrix in the file: %s", num2str(size(data)));
    x = data(:, 1);
    y = data(:, 2);
    warning ("avfiles", "min(x): %e", min(x));
    warning ("avfiles", "max(x): %e", max(x));
    warning ("avfiles", "min(y): %e", min(y));
    warning ("avfiles", "max(y): %e", max(y));
    [x, idx] = sort(x);
    y = y(idx);
    if k==1
      % use x values from the first file
      xp = x; yp = y;
    else
      yp = 1/k * ( (k-1)*yp + interp1(x, y, xp) );
      warning ("avfiles", "yp is undefined at %d points", sum(isnan(yp)));
    endif
endfor

idx = !isnan(yp);
dlmwrite("/dev/stdout", [xp(idx), yp(idx)], ' ');

%!test
%! n = 100;
%! for k=0:9
%!   x = unifrnd(0, 2*pi, n, 1);
%!   y = sin(x) + 0.05*stdnormal_rnd(n, 1);
%!   dlmwrite(sprintf("aux_file%i", k), [x y], " ");
%! end
%! system("octave -qf avfiles.m aux_file[0-9] > aux_out");
%! data = dlmread("aux_out");
%! x = data(:, 1); y = data(:, 2);
%! assert(sin(x), y, 0.08);

运行AVDEBUG=1 octave -qf avfiles.m file1.dat file2.dat以获取跟踪。有了你的示例文件,我得到:

warning: ================
warning: processing file: file1.dat
warning: size of the matrix in the file: 10    2
warning: min(x): 9.840000e+00
warning: max(x): 9.840639e+00
warning: min(y): 1.530000e+02
warning: max(y): 1.790000e+02
warning: ================
warning: processing file: file2.dat
warning: size of the matrix in the file: 10    2
warning: min(x): 9.840000e+00
warning: max(x): 9.840641e+00
warning: min(y): 1.630000e+02
warning: max(y): 1.750000e+02
warning: yp is undefined at 0 points
于 2013-04-13T17:59:15.013 回答