Is there any way to access a variable whose name is expressed by another string variable?

6 views (last 30 days)
Daiki
Daiki on 2 Aug 2021
Commented: darova on 5 Aug 2021
Is there any way in MATLAB to access a variable whose name is expressed by another string variable?
for example, when
myvar = 10;
string_myvar = "myvar";
were in the workspace and substituting the numerical scalar myvar into another myvar2 as
myvar2 = myvar;
Id’ like to find a way to give the right hand side "as the content of string scalar ‘string_myvar’ ".
  7 Comments

Sign in to comment.

Answers (1)

Chunru
Chunru on 5 Aug 2021
myvar = 10;
string_myvar = "myvar2";
assignin('base', string_myvar, myvar);
myvar2
myvar2 = 10

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by