Iteration for loop cell array storage

2 visualizzazioni (ultimi 30 giorni)
Serena Campbell
Serena Campbell il 27 Giu 2017
Risposto: Adam il 27 Giu 2017
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.

Risposte (1)

Adam
Adam il 27 Giu 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.

Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by