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

4 vues (au cours des 30 derniers jours)
Justin Callahan
Justin Callahan le 22 Oct 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.

Réponse acceptée

owr
owr le 22 Oct 2012
Modifié(e) : owr le 22 Oct 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 commentaire
Justin Callahan
Justin Callahan le 22 Oct 2012
Thank you very much owr. It's so simple, I should have thought of it myself! Thanks again!

Connectez-vous pour commenter.

Plus de réponses (1)

Walter Roberson
Walter Roberson le 22 Oct 2012

Catégories

En savoir plus sur Numeric Types dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by