function [errVec, len, indexVec] = read_dat_nfields(fname, nfields)
fid = fopen(fname, 'r');
errVec(1)=0;
indexVec(1)=1;
energy(1)=1;
len = 0;
while 1>0,
  
  [indexNow, count] = fscanf(fid,'%d',1);
  if count ~= 1,
     break;
  end;
  len=len+1;
  indexVec(len) = indexNow;
  [com, count] = fscanf(fid,'%s',3);
  if count ~= 3,
     break;
  end
  i=1;
  errVec(len,i) = fscanf(fid,'%f',1);
invalid =0;
for i=2:nfields,
  [com, count] = fscanf(fid,'%s',2);
  if count ~= 2,
    invalid = 1;
     break;
  end
  errVec(len,i) = fscanf(fid,'%f',1);
end
  if invalid==1,
    break;
  end
end;
fclose(fid);
return;
