MATLAB Answers

0

Repetitive anotation in plot

Asked by Alejandro Castro on 22 Oct 2019
Latest activity Commented on by Alejandro Castro on 25 Oct 2019 at 2:06
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

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.

Tags

1 Answer

Answer by Image Analyst
on 22 Oct 2019
Edited by Image Analyst
on 22 Oct 2019
 Accepted Answer

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));

  1 Comment

Thank you so much for your answer

Sign in to comment.