Continuously overwriting plot in a loop

조회 수: 54 (최근 30일)
Hans123
Hans123 2019년 4월 9일
편집: Peter Cook 2019년 4월 9일
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