Continuously overwriting plot in a loop
59 次查看(过去 30 天)
显示 更早的评论
Dear MATLAB Gods,
I have a plot in a loop that changes after each iteration, currently I have a hold on...hold off line which plots the graphs on the same figure, rather I need it to overwrite the current plot after each iteration and I need to visually see this too - i.e. see each figure before being updated(animated line)
How can I achieve this, and to see the plot being updated should I have a pause line?
This is what I have
-Another issue I have is the text I have writes over it after each iteration. How can I fix this issue. My code is pasted below
for k=1:length(Mean_step)
y1=Mean(k);
x1=Mean_step(k);
% yy=cap(cap>475 & cap<485);
% y1=min(yy);
y2=max_cap./10;
x2=0;
b=y2;
aaa=(x1*y1)+(y1)*(x2*y2-x1*y1)/(y1-y2);
bbb=(x2*y2-x1*y1)/(y1-y2);
dist=0:1/3:1600;
model=aaa./(dist + bbb);
plot(dist,model,'r-','Linewidth',1.5)
txt1 = ['Y = ' num2str(aaa) ' / (X + (' num2str(bbb) '))'];
text(580, 700, txt1,'FontSize',8);
end
0 个评论
采纳的回答
Peter Cook
2019-4-9
编辑:Peter Cook
2019-4-9
Since dist is the same for each loop iteration, and you are only updating model, you should return a handle to the line object before you start looping and just update the YData property. Similarly, you should just do the same for the text object.
y1 = Mean(1);
x1 = Mean_step(1);
y2 = max_cap./10;
x2 = 0;
b = y2;
aaa = (x1*y1)+(y1)*(x2*y2-x1*y1)/(y1-y2);
bbb = (x2*y2-x1*y1)/(y1-y2);
dist = 0:1/3:1600;
model = aaa./(dist + bbb);
hp = plot(dist, model, 'r-', 'Linewidth', 1.5);
txt1 = ['Y = ' num2str(aaa) ' / (X + (' num2str(bbb) '))'];
ht = text(580, 700, txt1, 'FontSize', 8);
for k = 2:length(Mean_step)
y1=Mean(k);
x1=Mean_step(k);
aaa=(x1*y1)+(y1)*(x2*y2-x1*y1)/(y1-y2);
bbb=(x2*y2-x1*y1)/(y1-y2);
model = aaa./(dist + bbb);
hp.YData = model;
txt1 = ['Y = ' num2str(aaa) ' / (X + (' num2str(bbb) '))'];
ht.String = txt1;
drawnow()
end
2 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Graphics Performance 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!