Repetitive anotation in plot

1 view (last 30 days)
I've got this code and I want it to show the position in x and y and other things but the problem is that it show the string 'Distancia Horizontal' and 'Distancia Vertical' correctly but it repeats the values of x and y, i.e. they accumulate.
while n<100
n = n + 1
t = 0:n
x=((vi*cos(angulo))/b)*(1-(e.^(-b*t)));
y=((1/b)*((g/b)+vi*sin(angulo))*(1-e.^(-b*t))-((g/b)*t))+altura;
hold on
plot(x,y)
title('Simulacion proyectiles Popocatepetl');
xlabel('Distancia Horizontal (m)');
ylabel('Distancia Vertical (m)');
str = {'Posicion en X' x,'Posicion en Y' y};
dim = [.2 .5 .3 .3];
delete(a)
a=annotation('textbox',dim,'String',str,'FitBoxToText','on');
drawnow;
pause(1)
grid on
if y<0
n=0
end
end
And the message:
  1 Comment
Walter Roberson
Walter Roberson on 22 Oct 2019
t = 0:n
so t is a vector.
x=((vi*cos(angulo))/b)*(1-(e.^(-b*t)));
y=((1/b)*((g/b)+vi*sin(angulo))*(1-e.^(-b*t))-((g/b)*t))+altura;
x and y are defined in terms of the vector t, so x and y will be vectors.
while n<100
n = n + 1
t = 0:n
n is increasing, so 0:n is increasing. So your x and y vectors are going to get larger, so your annotations are going to get larger.
Perhaps you want
str = {'Posicion en X' x(end), 'Posicion en Y', y(end)};

Sign in to comment.

Accepted Answer

Image Analyst
Image Analyst on 22 Oct 2019
Edited: Image Analyst on 22 Oct 2019
Try making x and y into strings. Or else just use sprintf() to build the whole string
str = sprintf('Posicion en X = %.3f, Posicion en Y = %.3f', x(end), y(end));
If you want them on separate lines, put in a backslash n:
str = sprintf('Posicion en X = %.3f\nPosicion en Y = %.3f', x(end), y(end));

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!