Simple Matlab Operation

Hi Guys! I have a noob question, that was bothering me. Let's say I have three variables: Var1=0, Var2=0, Var3=0. I also have another one - Var4=[1 2 3]
Is it possible to write this code in one row:
Var1=Var4(1);
Var2=Var4(2);
Var3=Var4(3);
Like it would be very useful if I could use something like this:
[Var1, Var2, Var3]=Var4(:);

 채택된 답변

Andrei Bobrov
Andrei Bobrov 2011년 6월 30일

1 개 추천

Var4 = num2cell([1 2 3])
[Var1, Var2, Var3]=Var4{:};
BUT it's don't useful

댓글 수: 4

Sean de Wolski
Sean de Wolski 2011년 6월 30일
FYI: num2cell is faster for scalar or dimensional divisions.
Matt Fig
Matt Fig 2011년 6월 30일
I re-iterate what andrei says. If you are making a bunch of variables with these names, you are probably off on the wrong track. Use cell arrays or another means to store vectors of different lengths. Please read FAQ 4.6.
Matt Fig
Matt Fig 2011년 6월 30일
http://matlab.wikia.com/wiki/FAQ#How_can_I_create_variables_A1.2C_A2.2C....2CA10_in_a_loop.3F
Artik Crazy
Artik Crazy 2011년 7월 1일
Thank you Andrei, Sean and Matt. It is useful in my application - the variables are already used in a code and their names should be self explanatory - they are not just Var1, Var2, but "MaxTension" , "PowerLimit" and etc.
Var4 is a long vector that is an input to a function and changes his values from time to time.

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Calendar에 대해 자세히 알아보기

제품

태그

Community Treasure Hunt

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

Start Hunting!

Translated by