How to save values as one variable from a for loop

조회 수: 1 (최근 30일)
Liyah
Liyah 2016년 4월 27일
댓글: Sana Ejaz 2019년 2월 25일
I have created a 'for' loop which effectively returns a value every time it runs. However, the previous value is overwritten when the 'for' loop executes again, leaving only the last value when the loop ends. I would like to retain all of the values in which the 'for' loop finds, so I can then plot all my data on one graph.
I am finding the branchpoints for multiple images and want all the branchpoints to be accessible in one variable/ vector.
I hope someone can help! Thanks in advance.

채택된 답변

J. Webster
J. Webster 2016년 4월 27일
save them in an array. Say you process the loop N times...
myArray = zeros(1,N); %initialize array to all zeros
for i=1:N
myArray(i) = getBranchPoint();
end
plot(myArray);
  댓글 수: 4
Liyah
Liyah 2016년 4월 27일
Okay great! I appreciate your help, the code worked well.
Sana Ejaz
Sana Ejaz 2019년 2월 25일
hey I have got the same probelem but when I used your method of declaring the array it give me an error
" Conversion to double from cell is not possible"
How to fix it?

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by