Is it possible to link an str answer with an existing variable?

1 回表示 (過去 30 日間)
A.G.
A.G. 2017 年 11 月 13 日
回答済み: Steven Lord 2017 年 11 月 13 日
I have 26 variables (one for each letter) which each contain a different matrix. After this I have a for loop that gives an output of a single letter. I want to link the variables and for loop in some way, so that if the loop gives off the letter 'a' it will output the matrix linked to the variable a instead of just the letter. Is this possible? And if it is how would it be accomplished?

採用された回答

Image Analyst
Image Analyst 2017 年 11 月 13 日
Use a switch statement or a multiple if statement, e.g.
switch letter
case 'a'
theMatrix = a;
case 'b'
theMatrix = b;
etc.
Now theMatrix will be one of those 26 other matrices according to what letter is in the 'letter' variable.

その他の回答 (1 件)

Steven Lord
Steven Lord 2017 年 11 月 13 日
Use a struct array and dynamic field names or getfield.
S = struct('a', [1 2 3], 'b', [4 5 6], 'c', [7 8 9]);
f = 'b';
x = S.(f) % will contain [4 5 6]
y = getfield(S, f) % will also contain [4 5 6]

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by