フィルターのクリア

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 Center および File ExchangeResizing and Reshaping Matrices についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by