Simple Question ( for loop with differnt variables)

1 vue (au cours des 30 derniers jours)
Max Müller
Max Müller le 15 Juil 2014
Commenté : Max Müller le 15 Juil 2014
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
............

Réponse acceptée

Azzi Abdelmalek
Azzi Abdelmalek le 15 Juil 2014
Modifié(e) : Azzi Abdelmalek le 15 Juil 2014
Why are you complicating things?
for i=1:3
A(i)=i
end
for i= 1:3
B(i) = A(i) + 1
end
  4 commentaires
Yoav Livneh
Yoav Livneh le 15 Juil 2014
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 le 15 Juil 2014
Thanks, Guys...it worked....guess i am going to sleep now.....

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Variables dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by