MATLAB: How to set multiple variables at once

multiple outputvariables

Hi, I wonder if I can set multiple variables at once. As far as I know, I cannot do that and I should set variables like
x = 1; y = 2;
I tried [x y] = [1 2]; as I use [x y] when I get multiple variables from function output, but it didn't work.
Is there any way to do this at once? I need this technique because I have many variables to set.
And I have one more question. What should I do if I want a variable change automatically when other variable changes?
for example,
x=1;y=2;A=[x y];x=3;
but matrix A does not change, it is still [1 2];
The questions seem quite fundamental but it would be a great help if I get the answer. Thank you.

Best Answer

[x, y] = deal(1, 2);
"What should I do if I want a variable change automatically when other variable changes?"
That is not recommended. The approaches that are recommended are:
  • Use functions instead of variables. For example you could define a class in which A was a "dependent property" that was calculated when you changed the property x of the object.
  • Parameterize an anonymous function; http://www.mathworks.com/help/matlab/math/parameterizing-functions.html
  • Use the Symbolic Toolbox to define A as a symbolic expression in terms of symbolic variable x. But (better) is not to assign a numeric value to x, and to instead subs() in a numeric value for x in the expression that is A: subs(A, x, 3)