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

Thread Subject: How to 'unnest' a cell array of cells?

Subject: How to 'unnest' a cell array of cells?

From: Dimitri Shvorob

Date: 09 Nov, 2007 08:36:17

Message: 1 of 11

Here's an example using output of REGEXP's token-search
facility:

string = 'blah bleh blah'
pattern = 'bl(.)h'
t = regexp(string,pattern,'tokens')

t =

    {1x1 cell} {1x1 cell} {1x1 cell}

>> t{:}

ans =

    'a'


ans =

    'e'


ans =

    'a'


class(t{1})

cell


I would like to have the tokens packed in a 'non-nested'
cell array. Is the loop below the best approach?

n = length(t);
c = cell(n,1);
for i = 1:n
    c{i} = cell2mat(t{i});
end
c

c =

    'a'
    'e'
    'a'

class(c{1})

char

Thank you.

Subject: Re: How to 'unnest' a cell array of cells?

From: Diederick Niehorster

Date: 09 Nov, 2007 09:57:39

Message: 2 of 11

Hi dimitri,

try [t{:}] on your nested cell.

- diederick

Subject: Re: How to 'unnest' a cell array of cells?

From: Dimitri Shvorob

Date: 09 Nov, 2007 12:30:21

Message: 3 of 11

Ooops! :) Thank you, Diederick.

Subject: Re: How to 'unnest' a cell array of cells?

From: Rik

Date: 06 Dec, 2007 15:06:13

Message: 4 of 11

But what would you suggest when there are multiple strings
on input:

My example:
strings = {'b=1 c=2'; 'b=2 c=1'; 'b=3 c=2'};
pattern = 'b=(\d) c=(\d)';
t = regexp(strings,pattern,'tokens');

>> [t{:}]

ans =

    {1x2 cell} {1x2 cell} {1x2 cell}

I am looking for the kind of output that is generated by:
[b,c] = cellfun(@getbc, strings)

b =

     1
     2
     3


c =

     2
     1
     2

with:

function [b,c] = getbc(str)
t = regexp(str, 'b=(\d) c=(\d)', 'tokens');
tt = [t{:}];
b = str2num(tt{1});
c = str2num(tt{2});

Anyway to do it without using cellfun?
Rik

Subject: Re: How to 'unnest' a cell array of cells?

From: Jason Breslau

Date: 06 Dec, 2007 16:07:37

Message: 5 of 11

Try using regexp's named tokens feature:

strings = {'b=1 c=2'; 'b=2 c=1'; 'b=3 c=2'};
t = regexp(strings, 'b=(?<b>\d) c=(?<c>\d)', 'names')
t = [t{:}]
b = [t.b]'
c = [t.c]'

Documentation of named tokens is here:
http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_prog/f0-42649.html#bqm94nz-1

-=>J

Subject: Re: How to 'unnest' a cell array of cells?

From: Rik

Date: 07 Dec, 2007 07:51:57

Message: 6 of 11

Thanks, but note that this doesn't work for b, c > 9:

clear all
strings = {'b=1 c=2'; 'b=2 c=10'; 'b=11 c=2'};
t = regexp(strings, 'b=(?<b>\d{1,}) c=(?<c>\d
{1,})', 'names');
t = [t{:}];
% the following does not produce the desired result:
b1 =[t.b]'
c1 =[t.c]'

% it requires something like
[bb{1:length(t)}] = deal(t.b);
[cc{1:length(t)}] = deal(t.c);
b2 = char(bb)
c2 = char(cc)

or ?


"Jason Breslau" <tendiamonds@mathworks.com> wrote in
message <fj96o9$hju$1@fred.mathworks.com>...
> Try using regexp's named tokens feature:
>
> strings = {'b=1 c=2'; 'b=2 c=1'; 'b=3 c=2'};
> t = regexp(strings, 'b=(?<b>\d) c=(?<c>\d)', 'names')
> t = [t{:}]
> b = [t.b]'
> c = [t.c]'
>
> Documentation of named tokens is here:
>
http://www.mathworks.com/access/helpdesk/help/techdoc/matla
b_prog/f0-42649.html#bqm94nz-1
>
> -=>J

Subject: Re: How to 'unnest' a cell array of cells?

From: Bruno Luong

Date: 07 Dec, 2007 08:03:48

Message: 7 of 11

cellfun(@(c) c{1}, t, 'UniformOutput', false)

Bruno

Subject: Re: How to 'unnest' a cell array of cells?

From: Bruno Luong

Date: 07 Dec, 2007 08:03:48

Message: 8 of 11

cellfun(@(c) c{1}, t, 'UniformOutput', false)

Bruno

Subject: Re: How to 'unnest' a cell array of cells?

From: Bruno Luong

Date: 07 Dec, 2007 08:03:59

Message: 9 of 11

cellfun(@(c) c{1}, t, 'UniformOutput', false)

Bruno

Subject: Re: How to 'unnest' a cell array of cells?

From: Jason Breslau

Date: 07 Dec, 2007 16:12:47

Message: 10 of 11

This will work:

strings = {'b=1 c=27'; 'b=2 c=1'; 'b=34 c=2'};
t = regexp(strings, 'b=(?<b>\d+) c=(?<c>\d+)', 'names');
t = [t{:}];
char(t.b)
char(t.c)

-=>J

"Rik " <rdewijn@xs4all.nl> wrote in message
<fjau2t$665$1@fred.mathworks.com>...
> Thanks, but note that this doesn't work for b, c > 9:
>
> clear all
> strings = {'b=1 c=2'; 'b=2 c=10'; 'b=11 c=2'};
> t = regexp(strings, 'b=(?<b>\d{1,}) c=(?<c>\d
> {1,})', 'names');
> t = [t{:}];
> % the following does not produce the desired result:
> b1 =[t.b]'
> c1 =[t.c]'
>
> % it requires something like
> [bb{1:length(t)}] = deal(t.b);
> [cc{1:length(t)}] = deal(t.c);
> b2 = char(bb)
> c2 = char(cc)
>
> or ?
>
>
> "Jason Breslau" <tendiamonds@mathworks.com> wrote in
> message <fj96o9$hju$1@fred.mathworks.com>...
> > Try using regexp's named tokens feature:
> >
> > strings = {'b=1 c=2'; 'b=2 c=1'; 'b=3 c=2'};
> > t = regexp(strings, 'b=(?<b>\d) c=(?<c>\d)', 'names')
> > t = [t{:}]
> > b = [t.b]'
> > c = [t.c]'
> >
> > Documentation of named tokens is here:
> >
> http://www.mathworks.com/access/helpdesk/help/techdoc/matla
> b_prog/f0-42649.html#bqm94nz-1
> >
> > -=>J
>

Subject: Re: How to 'unnest' a cell array of cells?

From: Rik

Date: 18 Dec, 2007 13:59:05

Message: 11 of 11


Thanks, it does ...

Interestingly, according to the "Quantifiers" table in the
Matlab regular expression
expr+ is equivalent to expr{1,}, but I guess here it isn't.

Rik

"Jason Breslau" <tendiamonds@mathworks.com> wrote in
message <fjbrdv$9h$1@fred.mathworks.com>...
> This will work:
>
> strings = {'b=1 c=27'; 'b=2 c=1'; 'b=34 c=2'};
> t = regexp(strings, 'b=(?<b>\d+) c=(?<c>\d+)', 'names');
> t = [t{:}];
> char(t.b)
> char(t.c)
>
> -=>J
>
> "Rik " <rdewijn@xs4all.nl> wrote in message
> <fjau2t$665$1@fred.mathworks.com>...
> > Thanks, but note that this doesn't work for b, c > 9:
> >
> > clear all
> > strings = {'b=1 c=2'; 'b=2 c=10'; 'b=11 c=2'};
> > t = regexp(strings, 'b=(?<b>\d{1,}) c=(?<c>\d
> > {1,})', 'names');
> > t = [t{:}];
> > % the following does not produce the desired result:
> > b1 =[t.b]'
> > c1 =[t.c]'
> >
> > % it requires something like
> > [bb{1:length(t)}] = deal(t.b);
> > [cc{1:length(t)}] = deal(t.c);
> > b2 = char(bb)
> > c2 = char(cc)
> >
> > or ?
> >
> >
> > "Jason Breslau" <tendiamonds@mathworks.com> wrote in
> > message <fj96o9$hju$1@fred.mathworks.com>...
> > > Try using regexp's named tokens feature:
> > >
> > > strings = {'b=1 c=2'; 'b=2 c=1'; 'b=3 c=2'};
> > > t = regexp(strings, 'b=(?<b>\d) c=(?<c>\d)', 'names')
> > > t = [t{:}]
> > > b = [t.b]'
> > > c = [t.c]'
> > >
> > > Documentation of named tokens is here:
> > >
> >
http://www.mathworks.com/access/helpdesk/help/techdoc/matla
> > b_prog/f0-42649.html#bqm94nz-1
> > >
> > > -=>J
> >
>

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
cell Dimitri Shvorob 09 Nov, 2007 03:40:23
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