Path: news.mathworks.com!not-for-mail
From: "Pekka " <pekka.nospam.kumpulainen@tut.please.fi>
Newsgroups: comp.soft-sys.matlab
Subject: Re: seperating numbers from a cell array
Date: Tue, 13 Nov 2007 08:23:00 +0000 (UTC)
Organization: Tampere University of Technology
Lines: 97
Message-ID: <fhbmt4$aig$1@fred.mathworks.com>
References: <f8kh7u$dll$1@fred.mathworks.com> <1185795493.093689.11280@o61g2000hsh.googlegroups.com> <f8n597$dp0$1@fred.mathworks.com> <f8n801$343$1@fred.mathworks.com>
Reply-To: "Pekka " <pekka.nospam.kumpulainen@tut.please.fi>
NNTP-Posting-Host: webapp-03-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1194942180 10832 172.30.248.38 (13 Nov 2007 08:23:00 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Tue, 13 Nov 2007 08:23:00 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 218565
Xref: news.mathworks.com comp.soft-sys.matlab:437255


"us " <us@neurol.unizh.ch> wrote in message 
<f8n801$343$1@fred.mathworks.com>...
> Sachitha Obeysekara:
> <SNIP down to gruesome ML atrocity...
> 
> > here's what i'm using:
> > regEx = '%s';
> > for i = 1:(numFieldsOut-1)
> >     regEx = [regEx '%n'];
> > end
> > b=textscan(b,regEx,'delimiter',',');
> > output = '[';
> > for i = 2:numFieldsOut
> >     output = [output 'b{1,' num2str(i) '} '];  
> > end
> > output = [output ']'];
> > output = eval(output);
> 
> WHY not simply use what you were given above?!...
> 
> % some data
>      b={
>           'T1,1,20,300,4000,00256.964'
>           'T10,1,20,300,4000,00356.964'
>           'TT1,10,200,3000,40000,00456'
>      };
>      nf=5; % #floats...
> % the engine
>      fmt=['%s',repmat('%f',1,nf)];
>      r=textscan(char(b).',fmt,'delimiter',',');
>      r=cat(2,r{:,2:end});
> % the result
>      b
>      r
> 
> us

Hi, the example above works fine. 
But if I have only numbers in my cell of characters, I get 
weird results.

b={
'10,20,3'
'4,7,23'
'9,6,1'}
nf = 3;
fmt=[repmat('%f',1,nf)];
r=textscan(char(b).',fmt,'delimiter',',')
>> r{1}

ans =

    10
     7
     6

>> r{2}

ans =

    20
    23
     1

>> r{3}

ans =

    34
     9

For some reason the last character of the first row is 
combined with the first one on the second row. But that 
doesn't happen on other rows. 
However if I add a fourth cell, it works right, for example

>> b={
'10,20,3'
'4,7,2'
'9,6,1'
'45,765,3'}
r=textscan(char(b).',fmt,'delimiter',',','collectoutput',1)
>> r{1}

ans =

    10    20     3
     4     7     2
     9     6     1
    45   765     3

The mysterious connection of the first and last rows also 
occurs if I have only two cells in b.

Is there something special in textscan for 3 or 2 lines of 
numbers or what is going on here?
I'm using 2007b