필터 지우기
필터 지우기

use assignin within a function

조회 수: 14 (최근 30일)
Oliver Warlow
Oliver Warlow 2013년 7월 1일
I want to create variable names automatically within a function (which are then used within this same function) that has been called by a script. As far as I am aware assign is unable to define variables within the workspace of the function.
Is there any neat solution to get round this? I do have a workaround but it is not very elegant. thanks Oli

답변 (2개)

Matt J
Matt J 2013년 7월 1일
편집: Matt J 2013년 7월 1일
You would use EVAL instead, but it is a bad idea to inexplicitly assign variables, whether by EVAL, ASSIGNIN, LOAD or whatever. Use dynamic fieldnames instead
S.('name1')=val1;
S.('name2')=val2;
etc... See also
and

Jan
Jan 2013년 7월 1일
This is a very frequently asked question, and the answer is easy: Do not do this. See FAQ: How can I create A1, A2, ... in a loop
Creating variables dynamically creates more problems as it solves. Beside the substantial decrease of the speed, the dugging is the main problem: It is hard to impossible to find out, what the program is doing when you read the code.
  댓글 수: 2
Oliver Warlow
Oliver Warlow 2013년 7월 1일
Thanks, reading some of those other examples shows that using dynamic variables is a much better idea so i'll go forward with that method.
Jan
Jan 2013년 7월 1일
Sorry, I haven't seen Matt J's answer when I typed mine. This is confusing because I hit the reload button directly before answering.
While "dynamic variables" are deprecated due to the explained reasons, "dynamic fieldnames" are efficient and clean.

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

카테고리

Help CenterFile Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by