Continuously overwriting plot in a loop

60 ビュー (過去 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 Center および File ExchangeGraphics Performance についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by