필터 지우기
필터 지우기

Update (x,y) position of moving object

조회 수: 15 (최근 30일)
monkey_matlab
monkey_matlab 2015년 11월 26일
댓글: monkey_matlab 2015년 11월 26일
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