Error with addpoints when trying to animate a trajectory following a marker
Show older comments
I am trying to animate a trajectory following a marker.
Here is the problematic part of my code:
figure(1)
axis([0, ceil(max(x)), 0, ceil(max(y))])
daspect([1 1 1])
grid on
xlabel('x (m)'), ylabel('y (m)'), title('Bouncing Projectile'), subtitle(['{\theta} = ', num2str(theta), '°, u = ', num2str(u), ' ms^{-1}, h = ', num2str(h), ' m, g = ', num2str(g), ' ms^{-2}, C = ', num2str(C), ', N = ', num2str(N)])
h = animatedline('Color', 'b');
p = plot(x(1), y(1), 'o', 'MarkerEdgeColor', 'red', 'MarkerFaceColor', 'red');
for k = 1:length(x)
p.XData = x(k);
p.YData = y(k);
addpoints(h, x(k), y(k));
drawnow limitrate
pause(dt)
end
Whithout the code for the marker, the program runs well. However, for some strange reason, adding the code for the marker seems to create an error:
Error using matlab.graphics.animation.AnimatedLine/addpoints
Value must be a handle.
Error in file (line 54)
addpoints(h, x(k), y(k));
A graph is produced when I run the program, however the axes are not labeled, there is no title, and only one point is plotted.
What mistake have I made?
8 Comments
Umar
on 20 Jul 2024
Edited: Walter Roberson
on 20 Jul 2024
Hi Felix,
The error "Value must be a handle" suggests that the handle used in the addpoints function is incorrect. Ensure that h is a valid handle to the graph object. To resolve this issue and improve the animation of the trajectory with a marker, I made some adjustments to the code.
function [x, y] = calculate_trajectory(theta, u, h, g, C, N, dt)
% Initialize arrays to store x and y coordinates
x = zeros(1, N);
y = zeros(1, N);
% Convert launch angle from degrees to radians
theta = deg2rad(theta);
% Calculate the trajectory using projectile motion equations
for i = 1:N
x(i) = u * cos(theta) * i * dt;
y(i) = h + u * sin(theta) * i * dt - 0.5 * g * (i * dt)^2;
% Check for ground collision and update y position accordingly
if y(i) < 0
y(i) = 0;
break;
end
end
end
% Define initial parameters
theta = 45; % Launch angle in degrees
u = 10; % Initial velocity in m/s
h = 0; % Initial height in meters
g = 9.81; % Acceleration due to gravity in m/s^2
C = 0; % Air resistance constant
N = 100; % Number of time steps
dt = 0.1; % Time step
% Calculate trajectory [x, y] = calculate_trajectory(theta, u, h, g, C, N, dt);
% Add red marker at the starting point p = plot(x(1), y(1), 'o', 'MarkerEdgeColor', 'red', 'MarkerFaceColor', 'red');
% Initialize the animated line
h = animatedline('Color', 'b');
addpoints(h, x(1), y(1));
for k = 2:length(x)
% Update the position of the red marker
p.XData = x(k);
p.YData = y(k);
% Add points to the animated line for the trajectory
addpoints(h, x(k), y(k));
drawnow limitrate
pause(dt)
end
% Plot the bouncing projectile trajectory at the end of animation
h = animatedline('Color', 'b', 'MaximumNumPoints', N);
addpoints(h, x(k), y(k));
drawnow limitrate
Feel free to customize the above code based on your preferences. Now, you should be able to figure out to resolve your problem.
Please see attached code snippet with plot.


Felix
on 20 Jul 2024
Umar
on 20 Jul 2024
Hi Felix,
No problem. I hope this answers your question.
Walter Roberson
on 20 Jul 2024
Please fix the formatting of your code.
When you are accessing using a mobile service, then the start of code is marked by an empty line followed by one or more spaces (preferably two spaces.) The code block then follows until the next empty line (even if the line has no leading spaces.)
I cannot fix it for you without losing the image you included.
Umar
on 20 Jul 2024
Hi Walter,
I tried following your instructions and it did not help. I remembered talking about this issue to one of the moderators and he couldn’t fix it. So, can you please help out, I will appreciate your help.
Walter Roberson
on 20 Jul 2024
I went onto mobile and fixed it. I inserted two spaces at the beginning of the function line, and two spaces at the begining of each block of code following an empty line -- for example two spaces at the beginning of % Calculate trajectory
Umar
on 20 Jul 2024
@Walter,
Could you please do me a favor, the code that you edited above, could you please execute this code on your mobile and provide a screenshot of the plot to see what results are you getting.
% Define initial parameters
theta = 45; % Launch angle in degrees
u = 10; % Initial velocity in m/s
h = 0; % Initial height in meters
g = 9.81; % Acceleration due to gravity in m/s^2
C = 0; % Air resistance constant
N = 100; % Number of time steps
dt = 0.1; % Time step
% Calculate trajectory
[x, y] = calculate_trajectory(theta, u, h, g, C, N, dt);
% Add red marker at the starting point
p = plot(x(1), y(1), 'o', 'MarkerEdgeColor', 'red', 'MarkerFaceColor', 'red');
% Initialize the animated line
h = animatedline('Color', 'b');
addpoints(h, x(1), y(1));
for k = 2:length(x)
% Update the position of the red marker
p.XData = x(k);
p.YData = y(k);
% Add points to the animated line for the trajectory
addpoints(h, x(k), y(k));
drawnow limitrate
pause(dt)
end
% Plot the bouncing projectile trajectory at the end of animation
h = animatedline('Color', 'b', 'MaximumNumPoints', N);
addpoints(h, x(k), y(k));
drawnow limitrate
function [x, y] = calculate_trajectory(theta, u, h, g, C, N, dt)
% Initialize arrays to store x and y coordinates
x = zeros(1, N);
y = zeros(1, N);
% Convert launch angle from degrees to radians
theta = deg2rad(theta);
% Calculate the trajectory using projectile motion equations
for i = 1:N
x(i) = u * cos(theta) * i * dt;
y(i) = h + u * sin(theta) * i * dt - 0.5 * g * (i * dt)^2;
% Check for ground collision and update y position accordingly
if y(i) < 0
y(i) = 0;
break;
end
end
end
Accepted Answer
More Answers (0)
Categories
Find more on Lengths and Angles 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!