如何使用不同的x和y值平均多个表文件

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

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

# 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绘制所有这些,但每张桌子都是自己的曲线我得到了这个

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

我尝试了join,但我得到一个空文件作为结果,我猜它不会工作,因为第一列(x值)在每个文件中不包含相同的值。试图用gnuplotplot data1 u 1:2 + data2 u 1:2进行总结也失败了。

我找到了merging multiple data files to plot in a single graph,但它只是合并文件没有帮助。


我正在使用Mac OS X但可以访问Ubuntu,所以如果有任何工具可以做到这一点......


脚本的新输出

> 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

如果您愿意,可以从qazxsw poi下载我的数据文件。平均值应该来自所有文件。

gnuplot octave
1个回答
1
投票

here (tweh.de/texsx/data-files-avarage.zip)很容易。创建文件octave。用法:

avfiles.m

octave -qf avfiles.m file1.dat file2.dat 使用测试数据octave -q --eval "test avfiles"创建文件。

avfiles.m:

aux_file[0-9]

运行#!/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
© www.soinside.com 2019 - 2024. All rights reserved.