How do you set a variable name to a variable value?
44 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
JIWON LEE
am 6 Aug. 2021
Kommentiert: Stephen23
am 6 Aug. 2021
If I make a variable ( a = 'name' ), I want to set the variable name with the value of a.
ex) ( name = )
4 Kommentare
Stephen23
am 6 Aug. 2021
Before you force yourself into writing slow, inefficient, complex code that is difficult to debug, you should read this:
Akzeptierte Antwort
Dave B
am 6 Aug. 2021
Bearbeitet: Dave B
am 6 Aug. 2021
a = 'Pi';
val = pi;
eval(a + " = " + val);
This link has some alternatives to using string evaluation to run code. It's a bad/dangerous style and there's almost always a better approach.
If you must name your variables with another variable, consider using fields of a struct rather than raw variables:
mydynvars=struct;
a = 'Pi';
b = 'e';
mydynvars.(a) = pi;
mydynvars.(b) = exp(1);
mydynvars
0 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Variables 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!