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

조회 수: 2 (최근 30일)
Justin Callahan
Justin Callahan 2012년 10월 22일
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.

채택된 답변

owr
owr 2012년 10월 22일
편집: owr 2012년 10월 22일
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
Justin Callahan
Justin Callahan 2012년 10월 22일
Thank you very much owr. It's so simple, I should have thought of it myself! Thanks again!

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Walter Roberson
Walter Roberson 2012년 10월 22일

카테고리

Help CenterFile Exchange에서 Numeric Types에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by