Simple Question ( for loop with differnt variables)

Heyho folks,
This is a really simple example for the Question I have.
for i=1:3
eval ( ['A' num2str(i) '=i'] )
end
generates A1 = 1 / A2 = 2 /A3 = 3
now i want to use these variables.....but how?
for i= 1:3
B = A{i} + 1
end %nope doesnt work
for i =1:3
B = ([A num2str(i)]) +1
end %nope doesnt work
............

 採用された回答

Azzi Abdelmalek
Azzi Abdelmalek 2014 年 7 月 15 日
編集済み: Azzi Abdelmalek 2014 年 7 月 15 日

0 投票

Why are you complicating things?
for i=1:3
A(i)=i
end
for i= 1:3
B(i) = A(i) + 1
end

4 件のコメント

Max Müller
Max Müller 2014 年 7 月 15 日
Well, the Problem is. This doesnt work.... I alraedy have the Variables A1 to A10 with Values. Now I want to use them as:
load('test.mat')
for i = 1:10
PlotData = A(i) % this is the important line
do some math.
end
So it takes A1 calculates....takes A2 calculates....
Azzi Abdelmalek
Azzi Abdelmalek 2014 年 7 月 15 日
for k=1:3
eval(['data=' sprintf('A%d',k)])
%do
end
Yoav Livneh
Yoav Livneh 2014 年 7 月 15 日
If you want to use A1, A2, etc. you will have to continue using eval. I recommend switching to an array or cell array, or even a structure, in order to better access your data.
Max Müller
Max Müller 2014 年 7 月 15 日
Thanks, Guys...it worked....guess i am going to sleep now.....

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および 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