変数の作成について

96 visualizaciones (últimos 30 días)
qrqr
qrqr el 21 de Feb. de 2019
Comentada: Shunichi Kusano el 22 de Feb. de 2019
以下のような変数があります。
TEST = 'set';
A = 1;
この 'set' を変数名にしてAを代入することはできますか?
%何かやり方
A = 1;
set = A;
= 1

Respuesta aceptada

Shunichi Kusano
Shunichi Kusano el 21 de Feb. de 2019
eval関数を使う方法があります。eval関数は中に入れた文字列が、あたかもコマンドとしてそのまま実行されるものです。
TEST = 'set';
A = 1;
command = [TEST, ' = A']; % 'set = A'
eval(command) % 'set = A' をコマンドとして実行する。
少し違いますが、構造体のフィールド名としてしまって、格納してしまうのも一つの手です。
your_struct.(TEST) = A;
  2 comentarios
madhan ravi
madhan ravi el 21 de Feb. de 2019
Editada: madhan ravi el 21 de Feb. de 2019
Shunichi Kusano
Shunichi Kusano el 22 de Feb. de 2019
This is quite useful information. Thanks!

Iniciar sesión para comentar.

Más respuestas (1)

madhan ravi
madhan ravi el 21 de Feb. de 2019
I strongly don’t recommend using eval here in this situation ,https://www.mathworks.com/help/matlab/matlab_prog/generate-field-names-from-variables.html - refer this as a workaround.
A=1;
s.set=A;
s.('set')

Categorías

Más información sobre プログラミング en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2013b

Community Treasure Hunt

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

Start Hunting!