我得到了一些包含相同测量结果的表文件,但由于差别不大,数据点得到了不同的 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 值)不包含相同的值每个文件。尝试使用gnuplot
在plot data1 u 1:2 + data2 u 1:2
中进行求和也会失败。
我找到merging multiple data files to plot in a single graph,但仅合并文件无效。
我正在使用Mac OS X但可以访问Ubuntu,所以如果有任何工具可以做到这一点......
剧本的新ouptut
> 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
如果您愿意,可以从here (tweh.de/texsx/data-files-avarage.zip)下载我的数据文件。平均值应该来自所有文件。
答案 0 :(得分:1)
octave很容易。创建文件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