MATLAB Answers

How to add 3th moving ball

조회 수: 2(최근 30일)
dmfwlansejr
dmfwlansejr 2021년 9월 10일
답변: Rik 2021년 9월 10일
2 Ball is moving
I want to add 3th moving ball (start position (0,1))
How to is it?
Following is My Code :(For operate, Copy & Paste)
x1_st=0;y1_st=0.5;
x2_st=0;y2_st=1.5;
%plot
h_fg=figure;
h_ax=axes(h_fg);
h_p1=plot(h_ax,x1_st,y1_st,'b.','MarkerSize',40); hold(h_ax,'on');
h_p2=plot(h_ax,x2_st,y2_st,'r.','MarkerSize',40); hold(h_ax,'on');
% change Properties
set(h_ax,'XLim',[-5 5]);
set(h_ax,'YLim',[0 5]);
h_ax.YLim=[0 2];
h_ax.XLim=[-5 5];
% for loop
dx=[1:0.1:5, -5:0.1:0];
for ii=1:length(dx)
x1_new=x1_st+dx(ii);
x2_new=x2_st-dx(ii);
set(h_p1,'XData',x1_new);
set(h_p2,'XData',x2_new);
pause(0.02);
end

답변(1개)

Rik
Rik 2021년 9월 10일
If you replace numbered variables with arrays you can use simple loops with indexing. With your current setup you will have to copy and paste every line of code referring to a ball.

Community Treasure Hunt

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

Start Hunting!

Translated by