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.

 채택된 답변

Stephen23
Stephen23 2018년 8월 14일
편집: Stephen23 2018년 8월 14일

3 개 추천

"I wonder if I can set multiple variables at once"
Not in the way that you are trying to do, because MATLAB is not Python (or whatever language you got that syntax from). It is much more efficient use of MATLAB to put values into arrays, and not to play around with variables like that. You could use a comma-separated list:
C = {1,2,3}
[x,y,z] = C{:}
"I need this technique because I have many variables to set."
Then you should put them into one numeric array, or one cell array, or one structure, or one table,... Magically assigning lots of values to variable names will force you into writing slow, complex, buggy MATLAB code. Read this to know why:
"What should I do if I want a variable change automatically when other variable changes?"
You can't, not like how you show: in MATLAB the value itself is assigned, not a handle or a reference to a value like in Python. If you want to learn how to use MATLAB effectively, then do not try to write code as if it was Python/yourFavoriteLanguage. In particular, forget about putting all of your data into lots of separate variables and learn to use arrays properly. Only then will MATLAB make any sense, and only then will you start to write efficient MATLAB code.

추가 답변 (3개)

Ruben D. Gonzalez L.
Ruben D. Gonzalez L. 2020년 9월 1일

3 개 추천

As of Matlab 2019a, the following works for me:
>> [x, y, z] = deal(1, 2, 3)
x =
1
y =
2
z =
3

댓글 수: 2

This has been possible for a long time. Before 2006.
Personally, I do not feel that using this is of value. It is both longer and less clear than
x = 1; y = 2; z = 3;
There are some useful circumstances for multiple assignment, mostly involving struct expansion or cell expansion. However, a number of years ago, MATLAB was enhanced to not need deal() for that purpose, as demonstrated in https://www.mathworks.com/matlabcentral/answers/414731-how-to-set-multiple-variables-at-once#answer_332664 .
I cannot agree more. Coming from Python, multiple assignment feel more natural to me, and I had just discovered deal() while developing object array interfaces and overriding subsref() and subsasgn() methods.
What I don't understand is why didn't I see your previous response from 2018. Otherwise, I wouldn't have wasted my humble two cents ;-)
Cheers.

댓글을 달려면 로그인하십시오.

Giseon Hong
Giseon Hong 2018년 8월 14일

0 개 추천

Thank you for answering me!
I could solve my problem with your answers!

카테고리

도움말 센터File Exchange에서 Call Python from MATLAB에 대해 자세히 알아보기

질문:

2018년 8월 14일

댓글:

2020년 9월 20일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by