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

3 Ansichten (letzte 30 Tage)
Justin Callahan
Justin Callahan am 22 Okt. 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.

Akzeptierte Antwort

owr
owr am 22 Okt. 2012
Bearbeitet: owr am 22 Okt. 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 Kommentar
Justin Callahan
Justin Callahan am 22 Okt. 2012
Thank you very much owr. It's so simple, I should have thought of it myself! Thanks again!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Walter Roberson
Walter Roberson am 22 Okt. 2012

Kategorien

Mehr zu Numeric Types finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by