string formatting - Matlab textscan introducing additional rows with zeros or NaNs -
i'm trying read .dat file containing tens of thousands of rows, each of them looks like:
1.9681968 0 0 19.996 0 61 100 1.94e-07 6.62e-07 2.330233 0 0 19.996 0 61 100 1.94e-07 6.62e-07 2.6512651 0 0 19.997 0 61 100 1.94e-07 6.62e-07 3.5923592 0 0 19.998 0 61 100 1.96e-07 6.62e-07
now example, i'm trying read with
data = textscan(fid, %.9f%*f%*f%.9f%*f%*f%*f%.9f)
where string format depends on column want read.
when reading big files, first column of cell array 'data' become
1.96819680000000 0 2.33023300000000 2.65126510000000 0 3.59235920000000 0
and rest of columns show nans instead of zeros. additional rows many rows in data file, arrays factor 2 larger.
i guess has errors when reading doubles, since this problem doesn't occur if try read file strings.
but if possible, not read strings , have convert doubles.
any ideas?
i think issue format string. try format string shown below.
fid = fopen('test.txt'); % data = textscan(fid, '%.9f%*f%*f%.9f%*f%*f%*f%.9f') data = textscan(fid, '%f %f %f %f %f %f %f %f %f'); data = cell2mat(data) fclose(fid);
where test.txt
text file containing given example data. above code gives following output.
1.9682 0 0 19.9960 0 61.0000 100.0000 0.0000 nan 2.3302 0 0 19.9960 0 61.0000 100.0000 0.0000 0.0000 2.6513 0 0 19.9970 0 61.0000 100.0000 0.0000 0.0000 3.5924 0 0 19.9980 0 61.0000 100.0000 0.0000 0.0000
notice nan
value when text contained 8 values. if want specify default value when lines contain less values use emptyvalue
setting:
data = textscan(fid, '%f %f %f %f %f %f %f %f %f','emptyvalue', 42);
then get:
1.9682 0 0 19.9960 0 61.0000 100.0000 0.0000 42.0000 2.3302 0 0 19.9960 0 61.0000 100.0000 0.0000 0.0000 2.6513 0 0 19.9970 0 61.0000 100.0000 0.0000 0.0000 3.5924 0 0 19.9980 0 61.0000 100.0000 0.0000 0.0000
you can first column indexing resulting matrix data(:,1)
outputs following:
1.9682 2.3302 2.6513 3.5924
Comments
Post a Comment