Insert variable name into another variable

19 ビュー (過去 30 日間)
Savannah Bifulco
Savannah Bifulco 2019 年 5 月 3 日
コメント済み: Rik 2019 年 5 月 3 日
I have a situation where I want one variable
Number=8;
And then I want to insert this number into various places in the rest of the code as such
Faces=Star[Number]Face;
How would I do this?
  1 件のコメント
Rik
Rik 2019 年 5 月 3 日
Save yourself from headaches and use something like what Matt suggested. You would need to use eval, which is a bad idea. (I have taken the liberty of adapting some of Stephen Cobeldick's thoughts on this topic)
Introspective programing (e.g. eval, assignin and other functions like it) is slow, and the MATLAB documentation warns specifically against it. Using eval is slow, makes debugging difficult, and removes all code helper tools (code checking, syntax hints, code completion, variable highlighting, etc.). A longer discussion can be found here. If you are not an expert and you think you need eval, there are probably better solutions to your problem. Also, if you are not an expert, you might find interesting and helpful hints on this page (and if you are an expert, you can still learn from it).

サインインしてコメントする。

回答 (1 件)

Matt J
Matt J 2019 年 5 月 3 日
編集済み: Matt J 2019 年 5 月 3 日
You would re-write all of your code to look like this instead
Faces=StarFace{Number}
or this
Faces=StarFace(Number)
See also,
  3 件のコメント
Matt J
Matt J 2019 年 5 月 3 日
編集済み: Matt J 2019 年 5 月 3 日
It means that you didn't read the link I gave you in its entirety ...
Rik
Rik 2019 年 5 月 3 日
It means what it says: you didn't define StarFace as either a variable or a class. You should read the link Matt posted. That will tell you to create a cell array and assign data to it, just as you were planning to assign data to Star8Face.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by