MATLAB: Reading in data with spaces

file readMATLABstringtext;textscan

I have a bunch of data with spaces in it that is fixed width. It is formatted such that each "column" is a fixed number of characters wide (including space characters). I've been trying to use "textscan" with a format like this: '%6c %6c %6c %6c %6c' but it seems to A)ignore spaces a the beginning of the string (this serves to throw off the 'count' when the first number goes from 9 to 10 for example) and B)not recognize blanks in the middle or at the ends. eg: "___8.5|___9.2|______|______|___7.6" where "_" represents one space and "|" are added for clarity. I need this to read in with 8.5 in the first cell, 9.2 in the second, and 7.6 in the 5th cell. I can tolerate it being a string (and converting later) as long as the column placement is preserved.

Best Answer

Here is a quick fix following a different path from what is mentioned in Dpb's thread. Assume that we have test.txt with the following content (attached):
1 9.3 8 7
12.4 2
Then running
buffer = fileread( 'test.txt' ) ;
buffer = regexprep( buffer, ' {6}', ' NaN' ) ;
data = reshape( sscanf( buffer, '%f' ), 5, [] )' ;
produces
>> data
data =
1.0000 9.3000 NaN 8.0000 7.0000
NaN 12.4000 2.0000 NaN NaN