Okay, this is a bit of a hack, but it works. textscan
can be so much faster than other methods that it is often worth it to play around a bit if your data has particular constraints.
fid = fopen('test.txt');
t = textscan(fid,'%s%*s%s%f%s%s','TreatAsEmpty','Q');
fclose(fid);
t{:}
You'll see that t{3}
is a 5-by-1 array with the default NaN
for the empty values. However, you still need to do one more thing as t{4}
is missing the leading 'Q'
for the first two elements. There are probably several ways to accomplish this, but here's an easy one-liner that uses isnan
to index into the rows where the 'Q'
needs to be added:
t{4}(isnan(t{3})) = cellfun(@(c)['Q' c],t{4}(isnan(t{3})),'UniformOutput',false);
How does using the 'TreatAsEmpty'
parameter work?
In the case of the fourth column (the third non-skipped column) we're dealing with a numeric field. This option only applies to when detecting numeric fields ('%f'
). The string 'Q25'
is broken in to the number NaN
and the string '25'
, effectively adding a column. The 'Q25'
elements in the fifth column don't matter because they're scanned as strings. So it should be fine if the letter 'Q'
appears elsewhere in the data.