Need help moving text inside of a figure window
조회 수: 28 (최근 30일)
이전 댓글 표시
Hey, Im trying to move a person's name (using input) and moving around in different directions in a figure window, but the text must hit the edges of the figure window like an old DVD logo type thing. Here is what i got so far:
%
clear
close all
clc
x = 0;
y = 0;
Name = input('Please input your name: ','s');
TH = text(x,y,Name);
axis([-1 1 -1 1]);
for i = 1:360
x = x + 0.01;
y= y + 0.01
set(TH,'Position', [x y 0]);
pause(0.01);
end
Thanks!
댓글 수: 0
채택된 답변
Sandro Lecci
2018년 5월 23일
편집: Sandro Lecci
2018년 5월 23일
Dear Jacob,
In your code you miss the instruction to change the direction of the text. The text object is defined only by the coordinates of the bottom left corner, so one would need to calculate the size of the text box and modify the value of the edge where the direction has to be changed. Therefore, long names need a "lower edge value" on the right.
clear
close all
clc
x = 0.4;
y = 0.0;
Name = input('Please input your name: ','s');
TH = text(x,y,Name);
axis([-1.3 1.3 -1 1]); %you need a rectangle for the "DVD-effect"
i = 1;
%define initial direction (up-right)
dx = 0.01;
dy = 0.01;
while i == 1
% Change UP-DOWN
if y == 0.95 %instruction to go down
dy = -0.01; %delta y
elseif y == -1%instruction to go up
dy = 0.01;
end
% Change LEFT-RIGHT
if x == -1.3 %instruction to turn right
dx = 0.01;
elseif x == 1.25 % instruction to turn left
dx = -0.01;
end
% Calculate new positions
x = x + dx;
y = y + dy;
x = round(x, 2);
y = round(y, 2);
%set new position
set(TH,'Position', [x y 0]);
pause(0.01);
end
The code above works but is not optimal, I let you find the best parameters.
Best, Sandro
댓글 수: 0
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Annotations에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!