Saving data for each iteration in a for loop

I am wanting to save the table data that is being written each loop, instead the data is being overwritten. I was going to try to do: info = [info output] that didn't work and I can't do info(i,:) = output because those deminsions do not line up. And the "table" varible I am not sure what I could do there to store all the data as oppsed to looping over it each time.
Thank you

답변 (2개)

Yongjian Feng
Yongjian Feng 2021년 8월 13일

1 개 추천

Your info needs to be initialized outside the loop, right?

댓글 수: 4

the data that needs to be I believe is outside of the loop.
I meant your info variable needs to be outside the loop. Something like thsi
info = [];
for i=1:9
info = [info output];
end
Gotcha, when I try that i get "Dimensions of arrays being concatenated are not consistent.
" due to the fact that each iteration the deminsions change.
Resolved it, got everything in a cell then did vertcat with said cells.

댓글을 달려면 로그인하십시오.

Cris LaPierre
Cris LaPierre 2021년 8월 13일
편집: Cris LaPierre 2021년 8월 13일

1 개 추천

You need to use indexing to assign the results of each loop to a new location. Look at this example on the for loop documentation page.
You may also want to consider going though Ch 13 of MATLAB Onramp.

댓글 수: 5

Yes I usally do that but since the values changes deminsions that indexing approach makes it a slightly trickier. Could possibly use cell arrays?
Cell arrays or structures could work.
You could also consider appending the new table to the existing one.
I am attempting the cell route running itno not supported varibles of this type. They are just doubles, not sure what types cells support am going to find out.
Share the code (copy/paste. No one wants to transcribe from a screenshot) along with the complete error message (all the red text).
Sorry about that, I actually just got it using cells however then I had to figure out how concatinate vectors of different number of rows. Vertcat came handy there.
Thank for all the help

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

질문:

2021년 8월 13일

댓글:

2021년 8월 13일

Community Treasure Hunt

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

Start Hunting!

Translated by