Getting the value of variable from its name
166 ビュー (過去 30 日間)
古いコメントを表示
I have variable name saved as a string in an array. I want to read the value of the variable into another array. Is there a function in matla which takes variable name as input and returns the value of that variable.
回答 (1 件)
Benjamin Kraus
2021 年 6 月 4 日
編集済み: Benjamin Kraus
2021 年 6 月 4 日
eval will do this, but see the link posted by Stephen explaining all the reasons you should avoid using eval: https://www.mathworks.com/matlabcentral/answers/304528-tutorial-why-variables-should-not-be-named-dynamically-eval
For example:
a = 1:10;
varName = 'a';
b = eval(varName);
1 件のコメント
Stephen23
2023 年 11 月 2 日
"see the link posted by Stephen explaining all the reasons you should avoid using eval:"
Correction: "see the link by Stephen which explains the main reasons why you should avoid forcing meta-data (e.g. pseudo-indices, test parameter values, etc.) into variable names or fieldnames."
参考
カテゴリ
Help Center および File Exchange で Cell Arrays についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!