Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I do multiple assignment in MATLAB?

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?

like image 822
Benjamin Oakes Avatar asked Feb 25 '10 19:02

Benjamin Oakes


People also ask

How do you declare multiple assignments in one?

You can assign the same value to multiple variables by using = consecutively. This is useful, for example, when initializing multiple variables to the same value. It is also possible to assign another value into one after assigning the same value.

How do you assign two variables in MATLAB?

Use comma-separated lists to get multiple variables in the left hand side of an expression. You can use deal() to put multiple assignments one line. [x,y] = deal(cell(4,8), cell(4,8)); Call it with a single input and all the outputs get the same value.

How do I display multiple items in MATLAB?

Display Multiple Variables on Same Line Here are three ways to display multiple variable values on the same line in the Command Window. Concatenate multiple character vectors together using the [] operator. Convert any numeric values to characters using the num2str function. Use disp to display the result.


Video Answer


1 Answers

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 
like image 66
Ramashalanka Avatar answered Sep 26 '22 05:09

Ramashalanka