Define multiple variables at the same time in MATLAB?
I don't know if MATLAB can do this, and I want it purely for aesthetics in my code, but can MATLAB create two variables at the same time?
Example
x = cell(4,8); y = cell(4,8);
Is there a way to write the code something similar to:
x&y = cell(4,8);
Answers
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.
[x,y] = deal( cell(4,8) ); >> [a,b,c] = deal( 42 ) a = 42 b = 42 c = 42
It depends on the function that you use to generate the data. You can create your own function in MATLAB that has more than one output:
[a, b, c] = foo();
Many builtin function also have this option. But this must be supported directly by the returning function.