fid = fopen( 'matlab.config.txt', 'r' );
dataSet = fscanf(fid,'%s',1);
output  = fscanf(fid,'%s',1);
runNum  = fscanf(fid,'%d',1);
engine  = fscanf(fid,'%s',1);
offset  = fscanf(fid,'%d',1);
fclose(fid);

FileString = dataSet;
Data = load(FileString);

[row,col] = size(Data);

if( (offset == 9) && (col < 11) )
  return;
end

fid = fopen(output,'a');
fprintf(fid,'%d ',runNum);
fprintf(fid,'%ld ',length(Data));

Horz = sqrt(Data(:,offset).^2 + Data(:,offset+1).^2);
HorzSort = sort(Horz);
per50   = HorzSort(floor(0.50*length(HorzSort)));
per68   = HorzSort(floor(0.68*length(HorzSort)));
per95   = HorzSort(floor(0.95*length(HorzSort)));
per96   = HorzSort(floor(0.96*length(HorzSort)));
per97   = HorzSort(floor(0.97*length(HorzSort)));
per98   = HorzSort(floor(0.98*length(HorzSort)));
per99   = HorzSort(floor(0.99*length(HorzSort)));
per995  = HorzSort(floor(0.995*length(HorzSort)));
per999  = HorzSort(floor(0.995*length(HorzSort)));
per9995 = HorzSort(floor(0.9995*length(HorzSort)));
per100  = HorzSort(end);

disp(['Epochs  = ',num2str(length(Horz)),10, ...
      ' 50%    = ',num2str(per50,'%7.3f'),'m', 10, ...
      ' 68%    = ',num2str(per68,'%7.3f'),'m', 10, ...
      ' 95%    = ',num2str(per95,'%7.3f'),'m', 10, ...
      ' 96%    = ',num2str(per96,'%7.3f'),'m', 10, ...
      ' 97%    = ',num2str(per97,'%7.3f'),'m', 10, ...
      ' 98%    = ',num2str(per98,'%7.3f'),'m', 10, ...
      ' 99%    = ',num2str(per99,'%7.3f'),'m', 10, ...
      ' 99.5%  = ',num2str(per995,'%7.3f'),'m', 10, ...
      ' 99.9%  = ',num2str(per999,'%7.3f'),'m', 10, ...
      ' 99.95% = ',num2str(per9995,'%7.3f'),'m', 10, ...
      '100%    = ',num2str(per100,'%7.3f'),'m' ]);

fprintf(fid,'%.3f %.3f %.3f %.3f %.3f %.3f %.3f %.3f %.3f %.3f %.3f %.3f ', ...
        per50, ...
        per68, ...
        per95, ...
        per96, ...
        per97, ...
        per98, ...
        per99, ...
        per995, ...
        per999, ...
        per9995, ...
        per100);

% Do the same for up
AbsHgt = abs(Data(:,offset+2));
HgtSort = sort(AbsHgt);
per50   = HgtSort(floor(0.50*length(HgtSort)));
per68   = HgtSort(floor(0.68*length(HgtSort)));
per95   = HgtSort(floor(0.95*length(HgtSort)));
per96   = HgtSort(floor(0.96*length(HgtSort)));
per97   = HgtSort(floor(0.97*length(HgtSort)));
per98   = HgtSort(floor(0.98*length(HgtSort)));
per99   = HgtSort(floor(0.99*length(HgtSort)));
per995  = HgtSort(floor(0.995*length(HgtSort)));
per999  = HgtSort(floor(0.995*length(HgtSort)));
per9995 = HgtSort(floor(0.9995*length(HgtSort)));
per100  = HgtSort(end);

disp(['Epochs  = ',num2str(length(AbsHgt)),10, ...
      ' 50%    = ',num2str(per50,'%7.3f'),'m', 10, ...
      ' 68%    = ',num2str(per68,'%7.3f'),'m', 10, ...
      ' 95%    = ',num2str(per95,'%7.3f'),'m', 10, ...
      ' 96%    = ',num2str(per96,'%7.3f'),'m', 10, ...
      ' 97%    = ',num2str(per97,'%7.3f'),'m', 10, ...
      ' 98%    = ',num2str(per98,'%7.3f'),'m', 10, ...
      ' 99%    = ',num2str(per99,'%7.3f'),'m', 10, ...
      ' 99.5%  = ',num2str(per995,'%7.3f'),'m', 10, ...
      ' 99.9%  = ',num2str(per999,'%7.3f'),'m', 10, ...
      ' 99.95% = ',num2str(per9995,'%7.3f'),'m', 10, ...
      '100%    = ',num2str(per100,'%7.3f'),'m' ]);

%fprintf(fid,'%.3f %.3f %.3f %.3f %.3f %.3f %.3f %.3f %.3f %.3f %.3f %.3f %s', ...
fprintf(fid,'%.3f %.3f %.3f %.3f %.3f %.3f %.3f %.3f %.3f %.3f %.3f %.3f ', ...
        per50, ...
        per68, ...
        per95, ...
        per96, ...
        per97, ...
        per98, ...
        per99, ...
        per995, ...
        per999, ...
        per9995, ...
        per100);
fprintf(fid,'%s\n',engine); 
%        per100, ...
%        engine);
%fprintf(fid,'\n');

fclose(fid);


