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

If I make a variable ( a = 'name' ), I want to set the variable name with the value of a.
ex) ( name = )

4 件のコメント

KSSV
KSSV 2021 年 8 月 6 日
name = 3 ;
name = 'John' ;
That's it....why you want to assign again a ? This approach is not accepted.
JIWON LEE
JIWON LEE 2021 年 8 月 6 日
I want to set variable name to 'John'
name = 'John'
value(name) = 3 --> John = 3;
KSSV
KSSV 2021 年 8 月 6 日
John = 3 ;
This is enough right.
Stephen23
Stephen23 2021 年 8 月 6 日
Before you force yourself into writing slow, inefficient, complex code that is difficult to debug, you should read this:

サインインしてコメントする。

 採用された回答

Dave B
Dave B 2021 年 8 月 6 日
編集済み: Dave B 2021 年 8 月 6 日

0 投票

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 件)

カテゴリ

ヘルプ センター および File ExchangeVariables についてさらに検索

タグ

質問済み:

2021 年 8 月 6 日

コメント済み:

2021 年 8 月 6 日

Community Treasure Hunt

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

Start Hunting!

Translated by