How do I combine multiple plots in a for loop?

74 ビュー (過去 30 日間)
Christopher
Christopher 2013 年 10 月 18 日
コメント済み: Rik 2019 年 6 月 11 日
Hello all,
I am trying to plot within a for loop, but it only plots the last value, as it's overwritting previous values. My code look like the following:
BLOCK = 5x1 cell array
TIME = 5x1 cell array
for i=length(BLOCK)
plot(TIME(i),BLOCK(i))
end
Not sure how to get this, so it doesn't overwrite previous values. Thank you
  1 件のコメント
Azzi Abdelmalek
Azzi Abdelmalek 2013 年 10 月 18 日
Christopher commented
I am trying to change the color for each i iteration though. This works, but gives me the same color plot for every iteration.

サインインしてコメントする。

採用された回答

Jonathan LeSage
Jonathan LeSage 2013 年 10 月 18 日
編集済み: MathWorks Support Team 2018 年 11 月 27 日
Add “hold on” prior to your for-loop.
Adding a "hold on" command means that anything that you plot will not clear the existing graph, but just plot on top of what is already there. You can turn off this functionality with the "hold off" command.
for k = 1:length(BLOCK)
plot(TIME(k),BLOCK(k))
if k == 1
hold on
end
end
hold off
  4 件のコメント
Christopher
Christopher 2013 年 10 月 21 日
Thank you Jonathan, that is exactly what I was looking to do. I have accepted your answer. Thanks to everyone else for there responses as well.
Rik
Rik 2019 年 6 月 11 日
Comment mistakenly posted as flag by AISWARYA LAKSHMI:
This code did not work for me! Only an empty plot screen appears :/

サインインしてコメントする。

その他の回答 (2 件)

dpb
dpb 2013 年 10 月 18 日
hold on
after first.

Hem Zaveri
Hem Zaveri 2014 年 11 月 1 日
I needed to plot graph of frames Vs pixel difference. And the plot was supposed to be inside the for loop. So after seeing this answer I solved one issue, that i update the graph using update. But i want all the points to be connected. Can you help me with it ?
Here is my code
for f = startingFrame+1 : finalFrame
frame = aviread(inFile, f);
i2 = frame.cdata; %cdata — The image data stored as a matrix of uint8 values. The dimensions of F.cdata are height-by-width-by-3.
ig2 = rgb2gray(i2);
[c2,n2] = imhist(ig2);
c2=c2/size(ig2,1)/size(ig2,2);
d = pdist2(c1',c2','cityblock');
if (d > 0.2150) %%change the threshold
c1 = c2;
m = im2frame(i2);
mov = addframe(mov,m);
pause(1/FPS);
end
disp(d);
hold on;
axis([1 95 0.16 0.26]);
plot(f,d,'--rs','LineWidth',2,...
'MarkerEdgeColor','k',...
'MarkerFaceColor','g',...
'MarkerSize',10);
end
f = frame, d = pixel difference which lies between 0.16 - 0.26 </matlabcentral/answers/uploaded_files/20313/asd.PNG>
  1 件のコメント
ang yong qin
ang yong qin 2017 年 3 月 16 日
Maybe you can store the data in a matrix first before plotting the points all together.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeAnnotations についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by