for loop for variables
48 views (last 30 days)
Show older comments
Nachiket Patki
on 9 May 2016
I have written one code as follows,
for i=1:10
Ai=i;
fprintf('A is: %d \n',Ai)
end
what i want to achieve from here is, I want to store 1,2,3..10 in variables A1,A2,A3...A10. But the code i have created is giving me Ai as variable instead A1,A2...etc and storing Ai=10 at the end.(It has also created row vector A=[1:10])
Accepted Answer
Azzi Abdelmalek
on 9 May 2016
Edited: Azzi Abdelmalek
on 9 May 2016
It's not a good idea to create variables A1,A2, ..., An. The array A=1:10 contains all your data. Why to create 10 variables, when one variable is sufficient
More Answers (1)
Weird Rando
on 9 May 2016
@Stephen: Haha I read the same thing somewhere online. But that didn't stop me :3
for i=1:10
eval(sprintf('A%i = i',i))
end
3 Comments
Weird Rando
on 10 May 2016
It been three years, and yeah that did happen. Can't even understand what I code.
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!