magiclen = 6625;
magiccol = 5;
n ='average.txt';
files = dir('*.txt');
tf = arrayfun(@(K) strcmp(n, files(K).name), 1:length(files));
files(tf) = [];
nfile = length(files);
if nfile == 0
error('There were no files found');
end
if nfile == 1;
warning(sprintf('Only one file found, only one column of output expected: %s', files(1).name);
end
Lat = zeros(magiclen, nfile)
for F = 1:nfile
thisfile = files(F).name;
try
f = load(thisfile);
catch me
error(sprintf('Failed loading file %s', thisfile));
end
if any(size(f) < [magiclen,magiccol])
error(sprintf('File %s loaded but is too small, only (%d by %d) but need (%d+ by %d+)', thisfile, size(f,1), size(f,2), magiclen, magiccol));
end
Lat(1:magiclen,F) = f(1:magiclen, magiccol);
end
fmt = ['\n' repmat('%15.6f', 1, nfile) '\n'];
fid = fopen(n, 'wt')
fprintf(fid, fmt, Lat.' );
fclose(fid);