Update (x,y) position of moving object

15 次查看(过去 30 天)
Hello,
I would like some insight as to how I can dynamically show the (x,y) points on a moving object in Matlab.
I am moving a ball around the origin and wanted to keep tracking the (x,y) coordinates (the center point of the ball) on the plot.
Here is my code:
close all; clc; clear;
% Define position of arm
theta=0:10:360; %theta is spaced around a circle (0 to 360).
r=0.03; %The radius of our circle.
%Define a circular magenta patch.
x=r*cosd(theta) + 0.075;
y=r*sind(theta) + 1;
% Size figure and draw arms
figure('position', [800, 300, 600, 550]);
myShape2=patch(x,y,'m');
set(myShape2,'Xdata',x,'Ydata',y);
axis([-1.5 3 -1 3]); grid on;
T=0.15; %Delay between images
for theta = pi/2:-pi/90:0,
if theta >= pi/4;
theta = theta;
else
theta = pi/2 - theta;
end
Arot = [sin(theta) cos(theta); -cos(theta) sin(theta)];
xyRot = Arot * [x; y]; % rotates the points by theta
xyTrans = xyRot; % translates all points by 0.1
set(myShape2,'Xdata',(x+xyTrans(1, :)),'Ydata',(y+xyTrans(2, :)));
text(x,y,['(' num2str(x) ',' num2str(y) ')'])
pause(T); %Wait T seconds
end

回答(1 个)

Stalin Samuel
Stalin Samuel 2015-11-26
clc
clear all
close all; clc; clear;
% Define position of arm
theta=0:10:360; %theta is spaced around a circle (0 to 360).
r=0.03; %The radius of our circle.
%Define a circular magenta patch.
x=r*cosd(theta) + 0.075;
y=r*sind(theta) + 1;
% Size figure and draw arms
figure('position', [800, 300, 600, 550]);
myShape2=patch(x,y,'m');
set(myShape2,'Xdata',x,'Ydata',y);
axis([-1.5 3 -1 3]); grid on;
T=0.5; %Delay between images
n = 1;
for theta = pi/2:-pi/90:0,
if theta >= pi/4;
theta = theta;
else
theta = pi/2 - theta;
end
Arot = [sin(theta) cos(theta); -cos(theta) sin(theta)];
xyRot = Arot * [x; y]; % rotates the points by theta
xyTrans = xyRot; % translates all points by 0.1
set(myShape2,'Xdata',(x+xyTrans(1, :)),'Ydata',(y+xyTrans(2, :)));
t = text((x(1)+xyTrans(1, n)),(y(1)+xyTrans(2, n)),num2str([(x(1)+xyTrans(1, n)) (y(1)+xyTrans(2, n)) ]),'FontSize',12)
pause(T); %Wait T seconds
delete(t)
n = n+1;
end
  1 个评论
monkey_matlab
monkey_matlab 2015-11-26
Hello, Thank you for your input. I get an error that the "Index exceeds matrix dimensions". This is due to the n = n+1 your suggestion has. Is there a way to fix this? Thanks again.

请先登录,再进行评论。

类别

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

标签

Community Treasure Hunt

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

Start Hunting!

Translated by