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?
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With