フィルターのクリア

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

5 ビュー (過去 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 Center および File ExchangeNumeric Types についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by