Clear Filters
Clear Filters

Update (x,y) position of moving object

15 views (last 30 days)
monkey_matlab
monkey_matlab on 26 Nov 2015
Commented: monkey_matlab on 26 Nov 2015
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

Answers (1)

Stalin Samuel
Stalin Samuel on 26 Nov 2015
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 Comment
monkey_matlab
monkey_matlab on 26 Nov 2015
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.

Sign in to comment.

Categories

Find more on Animation in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!