How to animate the plot back and forth for unlimited cycles?

7 ビュー (過去 30 日間)
Ahmed Anas
Ahmed Anas 2020 年 2 月 9 日
コメント済み: Mike Migliore 2022 年 2 月 4 日
Take the example of code pasted below.
I just want that the ball move back and forth again and again until some body closes the figure. I used 'while true' loop but it is not useful in this regard.
x = linspace(-1, 1, 75);
y = -x.^2;
figure(1)
for k2 = 1:2
for k1 = 1:length(x)
plot(x(k1)*(-1)^k2, y(k1), 'ob', 'MarkerFaceColor','r')
axis([min(x) max(x) min(y) max(y)])
refreshdata
drawnow
end
end

採用された回答

Subhadeep Koley
Subhadeep Koley 2020 年 2 月 10 日
Define a figure CloseRequestFcn like below
function my_closereq(src, callbackdata)
selection = questdlg('Close This Figure?',...
'Close Request Function',...
'Yes','No','Yes');
switch selection
case 'Yes'
delete(gcf);
case 'No'
return
end
end
And use this script
x = linspace(-1, 1, 75);
y = -x.^2;
fig = figure('CloseRequestFcn', @my_closereq);
ax = axes(fig);
while true
for k2 = 1:2
for k1 = 1:length(x)
plot(ax, x(k1)*(-1)^k2, y(k1), 'ob', 'MarkerFaceColor','r')
axis([min(x) max(x) min(y) max(y)])
refreshdata
drawnow
end
end
end
Hope this helps!
  2 件のコメント
Ahmed Anas
Ahmed Anas 2020 年 2 月 10 日
Yeah, it solved the issue... Thanks bro
Mike Migliore
Mike Migliore 2022 年 2 月 4 日
How would I go about altering this. I'd like the dot to switch directions rather than starting over from the initial position. So it would be bouncing back and forth instead.

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

その他の回答 (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