Is this the correct way to use "hold on" in a for loop?

1 view (last 30 days)
PASUNURU  SAI VINEETH
PASUNURU SAI VINEETH on 5 Aug 2021
Edited: VBBV on 5 Aug 2021
I am new to MATLAB and just trying to run a program for projection motion. I feel I am using the hold on and hold off in the correct way but I'm greeted with empty plot. Can someone please point out the mistake in the code?

Answers (3)

Yongjian Feng
Yongjian Feng on 5 Aug 2021
You only need to hold on for the first plot call, right?

Star Strider
Star Strider on 5 Aug 2021
Is this the correct way to use "hold on" in a for loop?
That is not the way I would do it.
Try this instead:
figure
hold on
for t = 1:5
dx = ...
dy = ...
plot(dx, dy, 'g-')
end
hold off
grid
.
  2 Comments
Star Strider
Star Strider on 5 Aug 2021
I canot run images of code, only actual code, so I could not run what was posted.
If you are plotting points, do this instead, in the loop:
plot(x, y, 'p')
to plot the points as stars without connecting lines.
.

Sign in to comment.


VBBV
VBBV on 5 Aug 2021
Edited: VBBV on 5 Aug 2021
%if true
for t = 1:5 ; 
dx(t) = vx*t;
dy(t) = vy*t-0.5*9.81*t^2; plot(dx(1:t),dy(1:t),'g-');
end

you are trying to plot points, dx and dy, are scalars, and using line as marker option in plot function. If you want to plot line you can try above.

Tags

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!