Got Questions? Get Answers.
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Non-intuitive behavior of cell assignment

Subject: Non-intuitive behavior of cell assignment

From: Darko

Date: 20 Jul, 2012 22:37:36

Message: 1 of 8

I get non-intuitive behavior when trying to assign part of a cell array. Version is MATLAB R2011b x64:

>> a={1, 2, 3};
>> a{1:2}
ans =
     1
ans =
     2
>> b=a{1:2}
b =
     1
>> size(b)
ans =
     1 1

That is, only first element gets assigned. Is this expected behavior and how can I assign part of a cell array (except in a loop).
Thanks
Darko

Subject: Non-intuitive behavior of cell assignment

From: dpb

Date: 20 Jul, 2012 23:45:01

Message: 2 of 8

On 7/20/2012 5:37 PM, Darko wrote:
> I get non-intuitive behavior when trying to assign part of a cell array.
> Version is MATLAB R2011b x64:
>
>>> a={1, 2, 3};
>>> a{1:2}
> ans =
> 1
> ans =
> 2
>>> b=a{1:2}
> b =
> 1
>>> size(b)
> ans =
> 1 1
>
> That is, only first element gets assigned. Is this expected behavior and
> how can I assign part of a cell array (except in a loop).

doc deal % for one way...

 >> [b{1:2}]=deal(a{1:2})
b =
     [1] [2]
 >>

--

Subject: Non-intuitive behavior of cell assignment

From: Darko

Date: 21 Jul, 2012 00:12:33

Message: 3 of 8

dpb <none@non.net> wrote in message <jucqhv$s0v$1@speranza.aioe.org>...
> On 7/20/2012 5:37 PM, Darko wrote:
> > I get non-intuitive behavior when trying to assign part of a cell array.
> > Version is MATLAB R2011b x64:
> >
> >>> a={1, 2, 3};
> >>> a{1:2}
> > ans =
> > 1
> > ans =
> > 2
> >>> b=a{1:2}
> > b =
> > 1
> >>> size(b)
> > ans =
> > 1 1
> >
> > That is, only first element gets assigned. Is this expected behavior and
> > how can I assign part of a cell array (except in a loop).
>
> doc deal % for one way...
>
> >> [b{1:2}]=deal(a{1:2})
> b =
> [1] [2]
> >>
>


Thanks, works like a charm.

Subject: Non-intuitive behavior of cell assignment

From: dpb

Date: 21 Jul, 2012 00:57:24

Message: 4 of 8

On 7/20/2012 7:12 PM, Darko wrote:
> dpb <none@non.net> wrote in message <jucqhv$s0v$1@speranza.aioe.org>...
...

>> doc deal % for one way...
>>
>> >> [b{1:2}]=deal(a{1:2})
>> b =
>> [1] [2]
>> >>
>>
>
>
> Thanks, works like a charm.

The problem otherwise is that a{1:2} is a 'comma-separated list' --
there's discussion under cells in the Data Types portion of the
documentation...

In my earlier release the syntax you used in your example gives a
warning of mismatched numbers of elements and aborts--I guess the silent
treatment and one element assigned is a later choice of behavior.

--

Subject: Non-intuitive behavior of cell assignment

From: Greg Heath

Date: 21 Jul, 2012 01:33:39

Message: 5 of 8

"Darko" wrote in message <jucmjg$bnc$1@newscl01ah.mathworks.com>...
> I get non-intuitive behavior when trying to assign part of a cell array. Version is MATLAB R2011b x64:
>
> >> a={1, 2, 3};
> >> a{1:2}
> ans =
> 1
> ans =
> 2
> >> b=a{1:2}
> b =
> 1
> >> size(b)
> ans =
> 1 1
>
> That is, only first element gets assigned. Is this expected behavior and how can I assign part of a cell array (except in a loop).

You can't even assign the whole array like that

>>a = {1 , 2, 3 }
a = [1] [2] [3]

>> a{:}
 ans = 1
 ans = 2
 ans = 3

>> b = a{:}
 b = 1

Seems to becaused by the 3 part response to a{:}

Greg

Subject: Non-intuitive behavior of cell assignment

From: Bruno Luong

Date: 21 Jul, 2012 06:34:08

Message: 6 of 8

dpb <none@non.net> wrote in message <jucupl$3sb$1@speranza.aioe.org>...
> On 7/20/2012 7:12 PM, Darko wrote:
> > dpb <none@non.net> wrote in message <jucqhv$s0v$1@speranza.aioe.org>...
> ...
>
> >> doc deal % for one way...
> >>
> >> >> [b{1:2}]=deal(a{1:2})
> >> b =
> >> [1] [2]

Note that b must exist before the assigment.

On recent MATLAB, syntax with DEAL on the rhs is not necessary

>> a={5 6 7 8}

a =

    [5] [6] [7] [8]

>> b=cell(1,2)

b =

    [] []

>> [b{:}] = a{2:3}

b =

    [6] [7]

% Bruno

Subject: Non-intuitive behavior of cell assignment

From: dpb

Date: 21 Jul, 2012 12:47:28

Message: 7 of 8

On 7/21/2012 1:34 AM, Bruno Luong wrote:
...

> On recent MATLAB, syntax with DEAL on the rhs is not necessary
>
>>> a={5 6 7 8}
>
> a =
> [5] [6] [7] [8]
>
>>> b=cell(1,2)
>
> b =
> [] []
>
>>> [b{:}] = a{2:3}
>
> b =
> [6] [7]
>
> % Bruno

That is indeed a help/step forward...can't come close here. Not much
other than deal() can handle the comma-list in this release (admittedly
getting quite long in the tooth by now, but being essentially that
meself :) there's no way can justify the big bucks TMW wants to upgrade)

 >> [b{:}]=deal(a{1:2})
??? Error using ==> deal
The number of outputs should match the number of inputs.

 >> [b{1:2}]=deal(a{1:2})
b =
     [1] [2]
 >> [b{:}]=a{1:2}
??? Illegal right hand side in assignment. Too many elements.

 >>

--

Subject: Non-intuitive behavior of cell assignment

From: Doug Schwarz

Date: 21 Jul, 2012 13:05:28

Message: 8 of 8

In article <jucmjg$bnc$1@newscl01ah.mathworks.com>,
 "Darko " <tanenbau_spam_@yahoo.com> wrote:

> I get non-intuitive behavior when trying to assign part of a cell array.
> Version is MATLAB R2011b x64:
>
> >> a={1, 2, 3};
> >> a{1:2}
> ans =
> 1
> ans =
> 2
> >> b=a{1:2}
> b =
> 1
> >> size(b)
> ans =
> 1 1
>
> That is, only first element gets assigned. Is this expected behavior and how
> can I assign part of a cell array (except in a loop).
> Thanks
> Darko


All you need is

b = a(1:2);

Use braces to reference cell contents. Parentheses are used to
reference the cells themselves.

--
Doug Schwarz
dmschwarz&ieee,org
Make obvious changes to get real email address.

Tags for this Thread

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.

Contact us