Drexel dragonThe Math ForumDonate to the Math Forum



Search All of the Math Forum:

Views expressed in these public forums are not endorsed by Drexel University or The Math Forum.


Math Forum » Discussions » Software » comp.soft-sys.matlab

Topic: textread/textscan not reading formatted text with whitespace
Replies: 19   Last Post: Oct 22, 2013 5:13 PM

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View   Messages: [ Previous | Next ]
dpb

Posts: 8,296
Registered: 6/7/07
Re: textread/textscan not reading formatted text with whitespace
Posted: Oct 22, 2013 10:06 AM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

On 10/22/2013 2:58 AM, Norbert Marwan wrote:
> 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','');
> fclose(fid);
> 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 ?


The above is one way to workaround to insert the delimiters; I'm not
fluent enough in regular expressions to decipher that w/o more effort to
understand precisely how it functions.

I'll stick it in my "saved-stuff" folder and try to dig into it at some
point and see if can get the principle down to understand how it could
be generalized.

Why it doesn't work in textread or textscan directly is because it is a
completely independent preprocessing step that modifies the input
subsequently given to them.

Again, I understand why TMW won't change the behavior of existing
functions--it's as outlined above they're based on C behavior and they
do follow C rules however bad the rules were that were chosen to define
C formatted input scanning (which essentially boil down to anything
other than delimited input isn't allowed).

The only way to ever get anywhere will be to continue to press for an
alternate input function that does handle the case -- maybe the idea
proposed should be to ask TMW to generalize the above solution to a
helper mex function that could take the input format of the data and do
the preprocessing automagically such that textscan and friends can
process it -- that would be a big step forward albeit still a poor
substitute for Fortran-like FORMAT i/o. The biggest problem with the
above is that regexp is notoriously slow; not sure how this particular
case will perform...

imo, $0.02, ymmv, etc., etc., etc., ...

--


Date Subject Author
10/2/13
Read textread/textscan not reading formatted text with whitespace
Norbert Marwan
10/2/13
Read Re: textread/textscan not reading formatted text with whitespace
dpb
10/2/13
Read Re: textread/textscan not reading formatted text with whitespace
Norbert Marwan
10/2/13
Read Re: textread/textscan not reading formatted text with whitespace
dpb
10/2/13
Read Re: textread/textscan not reading formatted text with whitespace
dpb
10/2/13
Read Re: textread/textscan not reading formatted text with whitespace
Derek Goring
10/2/13
Read Re: textread/textscan not reading formatted text with whitespace
dpb
10/2/13
Read Re: textread/textscan not reading formatted text with whitespace
dpb
10/3/13
Read Re: textread/textscan not reading formatted text with whitespace
Norbert Marwan
10/3/13
Read Re: textread/textscan not reading formatted text with whitespace
dpb
10/2/13
Read Re: textread/textscan not reading formatted text with whitespace
Norbert Marwan
10/2/13
Read Re: textread/textscan not reading formatted text with whitespace
dpb
10/3/13
Read Re: textread/textscan not reading formatted text with whitespace
Norbert Marwan
10/3/13
Read Re: textread/textscan not reading formatted text with whitespace
dpb
10/22/13
Read Re: textread/textscan not reading formatted text with whitespace
Norbert Marwan
10/22/13
Read Re: textread/textscan not reading formatted text with whitespace
dpb
10/22/13
Read Re: textread/textscan not reading formatted text with whitespace
dpb
10/22/13
Read Re: textread/textscan not reading formatted text with whitespace
Norbert Marwan
10/22/13
Read Re: textread/textscan not reading formatted text with whitespace
dpb
10/22/13
Read Re: textread/textscan not reading formatted text with whitespace
dpb

Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© Drexel University 1994-2014. All Rights Reserved.
The Math Forum is a research and educational enterprise of the Drexel University School of Education.