How do I do multiple assignment in MATLAB?

45,608

Solution 1

You don't need deal at all (edit: for Matlab 7.0 or later) and, for your example, you don't need mat2cell; you can use num2cell with no other arguments::

foo = [88, 12];
fooCell = num2cell(foo);
[x y]=fooCell{:}

x =

    88


y =

    12

If you want to use deal for some other reason, you can:

foo = [88, 12];
fooCell = num2cell(foo);
[x y]=deal(fooCell{:})

x =

    88


y =

    12

Solution 2

Note that deal accepts a "list" as argument, not a cell array. So the following works as expected:

> [x,y] = deal(88,12)
x = 88

y = 12

The syntax c{:} transforms a cell array in a list, and a list is a comma separated values, like in function arguments. Meaning that you can use the c{:} syntax as argument to other functions than deal. To see that, try the following:

> z = plus(1,2)
z = 3

> c = {1,2};
> z = plus(c{:});
z = 3

Solution 3

To use the num2cell solution in one line, define a helper function list:

function varargout = list(x)
% return matrix elements as separate output arguments
% example: [a1,a2,a3,a4] = list(1:4)

varargout = num2cell(x);

end

Solution 4

What mtrw said. Basically, you want to use deal with a cell array (though deal(88,12) works as well).

Assuming you start with an array foo that is n-by-2, and you want to assign the first column to x and the second to y, you do the following:

foo = [88,12;89,13;90,14];
%# divide the columns of foo into separate cells, i.e. do mat2cell(foo,3,[1,1])
fooCell = mat2cell(foo,size(foo,1),ones(size(foo,2),1));
[x,y] = deal(fooCell{:});

Solution 5

DEAL is really useful, and really confusing. foo needs to be a cell array itself, I believe. The following seems to work in Octave, if I remember correctly it will work in MATLAB as well:

> foo = {88, 12}
foo =

{
  [1,1] =  88
  [1,2] =  12
}

> [x,y] = deal(foo{:})
x =  88
y =  12
Share:
45,608

Related videos on Youtube

Benjamin Oakes
Author by

Benjamin Oakes

Ruby, JavaScript, and Web. If I made a contribution that helped you, please consider Flattr logo http://api.flattr.com/button/flattr-badge-small.png leaving a tip via Flattr. Maid I maintain the open source Maid project: Be lazy. Let Maid clean up after you, based on rules you define. It's easy to install on a Mac or Linux. See instructions at http://rubygems.org/gems/maid TabCarousel I maintain an open source Chrome extension to help you keep tabs on info you want to monitor. Great for a TV or other external display. Install it from the Chrome Web Store.

Updated on May 01, 2020

Comments

  • Benjamin Oakes
    Benjamin Oakes about 4 years

    Here's an example of what I'm looking for:

    >> foo = [88, 12];
    >> [x, y] = foo;
    

    I'd expect something like this afterwards:

    >> x
    
    x =
    
        88
    
    >> y
    
    y =
    
        12
    

    But instead I get errors like:

    ??? Too many output arguments.
    

    I thought deal() might do it, but it seems to only work on cells.

    >> [x, y] = deal(foo{:});
    ??? Cell contents reference from a non-cell array object.
    

    How do I solve my problem? Must I constantly index by 1 and 2 if I want to deal with them separately?

    • Justin Peel
      Justin Peel about 14 years
      Deal works only if foo is a cell. You have defined foo as a standard array. That's why you got the ??? Cell contents reference from a non-cell array object. error message.
  • Justin Peel
    Justin Peel about 14 years
    Just a side note, I think you can only get away with not using deal( as in the first example) in Matlab 7+.
  • Jonas
    Jonas about 14 years
    Interesting. I didn't know that deal is unnecessary nowadays. However, I used mat2cell on purpose, since I assume that the OP might want to separate columns from each other.
  • Benjamin Oakes
    Benjamin Oakes about 14 years
    This is really good. But is there any way to have it all on one line? Maybe something like: [x y] = num2cell(foo){:} (Sorry, still confused by Matlab quite often.)
  • Ramashalanka
    Ramashalanka about 14 years
    @Justin: good point, you need version 7.0 (2004) or later. @Jonas: true, mat2cell is good if you want to split up in different ways. @Benjamin: I'm not aware of a one line method, unless you use a cell to begin with; you can use deal(88,12) if you are starting from scalars. It'd be good if we stuff like num2cell(foo){:} for many Matlab commands.
  • zhangxaochen
    zhangxaochen about 9 years
    won't the example [a1,a2,a3,a4] = list(1:4) cause Too many output arguments error?