MATLAB Answers

Repetitive anotation in plot

3 views (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
hold on
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];
grid on
if y<0
And the message:

  1 Comment

Walter Roberson
Walter Roberson on 22 Oct 2019
t = 0:n
so t is a vector.
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));

  1 Comment

Alejandro Castro
Alejandro Castro on 25 Oct 2019
Thank you so much for your answer

Sign in to comment.

More Answers (0)


Community Treasure Hunt

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

Start Hunting!