Date: Oct 22, 2013 3:58 AM
Author: Norbert Marwan
Subject: Re: textread/textscan not reading formatted text with whitespace

So, after some mail exchange with the Matlab support I got a solution. In my opinion, it is not very convenient but finally it works:

fid = fopen('test.txt');
tmp1 =textscan(fid,'%s','delimiter','');
tmp = tmp1{:};
% this can read input fields with predefined width! it adds commas between these fields
tmp = cellfun(@(x)regexprep(x,'(....)(..)(..)(...)(...)','$1,$2,$3,$4,$5'),tmp,'UniformOutput',false);
a = cellfun(@(x)textscan(x,'%4.0f%2.0f%2.0f%3.0f%3.0f','delimiter',','),tmp,'UniformOutput',false);
a = cellfun(@(x)cell2mat(x),a,'UniformOutput',false);
a = cell2mat(a);

The interesting point here is '(....)(..)(..)(...)(...)' which allows considering the fixed field width. I wonder why such is not working in textread or textscan directly ?

I mentioned that textread and textscan is confusing because it pretends such functionality but cannot do it. The ability to use formatted strings in textread or textscan is completely useless because it nevertheless needs field separators. But then working with field separators is mostly sufficient.

The support again insisted to not change these functions. I do not why. It seems that Mathworks is afraid to realise suggestions from its customers ?