How to save values as one variable from a for loop

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 日

0 投票

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 日
Thank you for such a speedy reply. Would this be placed inside the for loop I already have or as a separate loop?
Thanks
J. Webster
J. Webster 2016 年 4 月 27 日
my answer is just to show you the general method. Declare the array outside the loop, and update it inside the loop.
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 件)

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

質問済み:

2016 年 4 月 27 日

コメント済み:

2019 年 2 月 25 日

Community Treasure Hunt

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

Start Hunting!

Translated by