Getting the value of variable from its name

조회 수: 161 (최근 30일)
Pushkar Dhekne
Pushkar Dhekne 2021년 6월 4일
댓글: Stephen23 2023년 11월 2일
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
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
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 CenterFile Exchange에서 Cell Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by