Create variable with name from other variable string
50 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Victor Andrés Sánchez Zurita
am 26 Jun. 2021
Bearbeitet: Stephen23
am 28 Jun. 2021
Hello,
My question is, if i have a string variable, coulf i assing that string as a name for a new variable?
For example, if i have
name_string = 'name_var';
can i have
name_var = 5;
somehow wihouth me writing the actual code?
Akzeptierte Antwort
Joseph Cheng
am 26 Jun. 2021
While it is not adviseable to have variables created in this way maybe you have a good reason to but still un-adviseable but here you go:
name_str = 'name_var';
eval(sprintf('%s = 5',name_str))
disp([name_str 'has been assigned to ' num2str(name_var)])
3 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Startup and Shutdown finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!