Iteration for loop cell array storage

2 visualizaciones (últimos 30 días)
Serena Campbell
Serena Campbell el 27 de Jun. de 2017
Respondida: Adam el 27 de Jun. de 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.

Respuestas (1)

Adam
Adam el 27 de Jun. de 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.

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by