function [errVec, len] = read_matrix_nfields(fname, nfields)
fid = fopen(fname, 'r');
errVec(1)=0;
len = 0;
while 1>0,
  
  
    [xx, count] = fscanf(fid,'%f',1);
    if count ~= 1,
     break;
    end;
    len=len+1;
  
  i=1;
  errVec(len,i) = xx;
  invalid =0;
  for i=2:nfields,
   errVec(len,i) = fscanf(fid,'%f',1);
end
end;
fclose(fid);
return;
