Iterate through alphabetical names

1 回表示 (過去 30 日間)
Marc
Marc 2014 年 5 月 22 日
編集済み: Marc 2014 年 7 月 16 日
Hello there,
maybe it's a dumb question, but how do I iterate through alphabetical names? Assuming I have matrices A to D, then how can I use those names in a loop?
My approach would be to create a string
names='ABCD'
and loop through those in a loop
for i=0:4
names(i)
end for
But how can I use this for example for the following operation?
boxplot(A(:))
If I simply use
names='ABCD';
for i=0:4
boxplot(name(i)(:))
end for
Then it doesn't work, of course. strcat does not do the job either.
I hope someone has an idea :)
Cheers

採用された回答

Star Strider
Star Strider 2014 年 5 月 22 日
Not dumb at all.
You need to use the eval function in your loop:
A = rand(2,10);
B = rand(3,10);
C = rand(4,10);
D = rand(5,10);
names = 'ABCD';
for k1 = 1:length(names)
figure(k1)
boxplot(eval(names(k1)))
end

その他の回答 (2 件)

Marc
Marc 2014 年 5 月 22 日
That's great, thank you!
  1 件のコメント
Star Strider
Star Strider 2014 年 5 月 22 日
My pleasure!

サインインしてコメントする。


Marc
Marc 2014 年 5 月 22 日
Okay one more question ... how can I access a variable using a predefined name vector?
for i=1:length(models)
fprintf('\t Open %s\n', models{i})
eval(models(i))=xlsread(strcat(path,'\',eval(models(i)),'_PER.xlsx'));
end
Eval does not do the job here ?
  4 件のコメント
Marc
Marc 2014 年 5 月 22 日
Okay - in the meantime I switched to use structs instead of regular variables ... I thought too much in common programming languages ;)
Star Strider
Star Strider 2014 年 5 月 22 日
See if evalin will do what you want.

サインインしてコメントする。

カテゴリ

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