8

Possible Duplicate:
How do I do multiple assignment in MATLAB?

When dealing with cell arrays, I can use the deal() function to assign cells to output variables, such as:

[a, b, c] = deal(myCell{:});

or just:

[a, b, c] = myCell{:};

I would like to do the same thing for a simple array, such as:

myArray = [1, 2, 3];
[a, b, c] = deal(myArray(:));

But this doesn't work. What's the alternative?

Community
  • 1
  • 1
jjkparker
  • 27,597
  • 6
  • 28
  • 29
  • 4
    I thought this question sounded familiar, but it took me some time to find the duplicate: http://stackoverflow.com/questions/2337126/multiple-assignment-in-matlab. I think I'll try to tag these questions better when I get a chance. – gnovice Apr 29 '10 at 21:20

2 Answers2

10

One option is to convert your array to a cell array first using NUM2CELL:

myArray = [1, 2, 3];
cArray = num2cell(myArray);
[a, b, c] = cArray{:};

As you note, you don't even need to use DEAL to distribute the cell contents.

gnovice
  • 125,304
  • 15
  • 256
  • 359
0

Not terribly pretty, but:

myArray = 1:3;
c = arrayfun(@(x) x, myArray , 'UniformOutput', false); 
c{:}
Richie Cotton
  • 118,240
  • 47
  • 247
  • 360
  • 1
    Actually, scratch that, the `arrayfun` call basically does the same thing as `num2cell`. – Richie Cotton Apr 30 '10 at 13:21
  • 1
    Here's a trick for shorter array/cell/Xfun calls: instead of 'UniformOutput',false, you can just have your anonymous function return a cell. c = arrayfun(@(x) {x}, myArray) – Andrew Janke Apr 30 '10 at 13:40