Iteration for loop cell array storage
2 views (last 30 days)
Show older comments
I am doing a for loop that outputs a matrix "R" every iteration. I wanted to make a cell array to store each matrix however it only stores the final output i.e. overwrites the cell every iteration - any help?
ct=0;
for diamrod1 = 10e-9:5e-9:20e-9;
[r,t,R,T]=fv_trans_refl_2D_slabs(F, materials, d, pol);
ct=ct+1;
Rcollected{ct,1}=R;
end
after I run it the Rcollected cell only has a saved matrix at ct=3.
0 Comments
Answers (1)
Adam
on 27 Jun 2017
This seems rather obvious, but it is often the obvious things that are hardest to see...
Nothing in your for loop depends on the diamrod1 loop variable. In particular this line:
[r,t,R,T]=fv_trans_refl_2D_slabs(F, materials, d, pol);
will evaluate to the same thing every time round the loop. This still would not explain why only ct=3 stores a matrix, but it would explain why all matrices stored would be the same.
0 Comments
See Also
Categories
Find more on Loops and Conditional Statements 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!