Need help moving text inside of a figure window
28 views (last 30 days)
Show older comments
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 Comments
Accepted Answer
Sandro Lecci
on 23 May 2018
Edited: Sandro Lecci
on 23 May 2018
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 Comments
More Answers (0)
See Also
Categories
Find more on Annotations in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!