How do I do multiple assignment in MATLAB?
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
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
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