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
matlabissue.png
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

采纳的回答

Peter Cook
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 个评论
Hans123
Hans123 2019-4-9
Thanks alot for the reply, I am a novice still so I do not fully grasp the meaning of ht.YData or ht.String
Also, after running the code I get this error,
Unrecognized property 'YData' for class 'matlab.graphics.primitive.Text'.
Error in DynamicMathModel (line 203)
ht.YData = model;
Peter Cook
Peter Cook 2019-4-9
Oops It should be hp.YData & ht.String. I'll edit above to reflect that.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Graphics Performance 的更多信息

标签

产品

Community Treasure Hunt

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

Start Hunting!

Translated by