How do you set a variable name to a variable value?

68 次查看(过去 30 天)
If I make a variable ( a = 'name' ), I want to set the variable name with the value of a.
ex) ( name = )
  4 个评论

请先登录,再进行评论。

采纳的回答

Dave B
Dave B 2021-8-6
编辑:Dave B 2021-8-6
You can do this with eval, but you are likely to regret this.
a = 'Pi';
val = pi;
eval(a + " = " + val);
Pi = 3.1416
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
mydynvars = struct with fields:
Pi: 3.1416 e: 2.7183

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Whos 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by