How to use the value of a variable as a variable?

3 visualizaciones (últimos 30 días)
Justin Callahan
Justin Callahan el 22 de Oct. de 2012
I will try to describe this the best I can. I have a variable A and its value is a string, lets say BC.
A = BC;
The value of A (BC in this case) is determined within a loop and changes. However, I want string BC to be turned into a variable where I can save more information. Specifically I want BC to be turned into a matrix that I can use later on.
Anyone have any tips on how I can do this?
Thank you for your time.

Respuesta aceptada

owr
owr el 22 de Oct. de 2012
Editada: owr el 22 de Oct. de 2012
You could use a structure and define fields indirectly using the current value of "A":
>> A = 'BC'
A =
BC
>> s.(A) = randn(3,3)
s =
BC: [3x3 double]
>> A = 'CD'
A =
CD
>> s.(A) = randn(4,4)
s =
BC: [3x3 double]
CD: [4x4 double]
You can access the fields using the dynamically defined names:
>> s.BC
ans =
1.4090 -1.2075 0.4889
1.4172 0.7172 1.0347
0.6715 1.6302 0.7269
>> s.CD
ans =
-0.3034 -1.1471 1.4384 -1.7115
0.2939 -1.0689 0.3252 -0.1022
-0.7873 -0.8095 -0.7549 -0.2414
0.8884 -2.9443 1.3703 0.3192
  1 comentario
Justin Callahan
Justin Callahan el 22 de Oct. de 2012
Thank you very much owr. It's so simple, I should have thought of it myself! Thanks again!

Iniciar sesión para comentar.

Más respuestas (1)

Walter Roberson
Walter Roberson el 22 de Oct. de 2012

Categorías

Más información sobre Variables en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by