Skip to Main Content Skip to Search
Login
File Exchange
MATLAB Newsgroup
Link Exchange
  Blogs  
 Contest 
MathWorks.com

Thread Subject: seperating numbers from a cell array

Subject: seperating numbers from a cell array

From: Sachitha Obeysekara

Date: 30 Jul, 2007 11:15:10

Message: 1 of 5

hi,
i have a cell array:

b =

T1,0,0,0,0,00256.964
T1,0,0,0,0,00256.656
T1,0,0,0,0,00256.869
T1,0,0,0,0,00257.666
T1,0,1,1,0,00246.296
...and so on

how do i seperate out so that i have a array like this:
c =

0 0 0 0 00256.964
0 0 0 0 00256.656
0 0 0 0 00256.869
0 0 0 0 00257.666
0 1 1 0 00246.296
..and so on

the T1 field can be discarded

Cheers

Subject: Re: seperating numbers from a cell array

From: JWagberg

Date: 30 Jul, 2007 04:38:13

Message: 2 of 5

On Jul 30, 1:15 pm, "Sachitha Obeysekara" <sachitha.
2.obeysek...@gsk.com> wrote:
> hi,
> i have a cell array:
>
> b =
>
> T1,0,0,0,0,00256.964
> T1,0,0,0,0,00256.656
> T1,0,0,0,0,00256.869
> T1,0,0,0,0,00257.666
> T1,0,1,1,0,00246.296
> ...and so on
>
> how do i seperate out so that i have a array like this:
> c =
>
> 0 0 0 0 00256.964
> 0 0 0 0 00256.656
> 0 0 0 0 00256.869
> 0 0 0 0 00257.666
> 0 1 1 0 00246.296
> ..and so on
>
> the T1 field can be discarded
>
> Cheers

One of many solutions:

tmp=textscan(char(b)','%*s%n%n%n%n%n','delimiter',',');
c=cat(2,C{:});

The above solution has some generality but must be modified if the
number of fields changes.

HTH,

jewa & wagberg , net (Modify the obvious)


Subject: Re: seperating numbers from a cell array

From: Sachitha Obeysekara

Date: 31 Jul, 2007 11:09:28

Message: 3 of 5

thanks

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);

Subject: Re: seperating numbers from a cell array

From: us

Date: 31 Jul, 2007 11:55:45

Message: 4 of 5

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

Subject: Re: seperating numbers from a cell array

From: Pekka

Date: 13 Nov, 2007 08:23:00

Message: 5 of 5

"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

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
atrocity us 31 Jul, 2007 08:00:21
textscan us 31 Jul, 2007 08:00:21
code us 31 Jul, 2007 08:00:21
array Sachitha Obeysekara 30 Jul, 2007 07:20:18
cell Sachitha Obeysekara 30 Jul, 2007 07:20:18
rssFeed for this Thread

envelope graphic E-mail this page to a colleague

Public Submission Policy
NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Disclaimer prior to use.
Related Topics