Skip to content

Commit a9e3d58

Browse files
fixing animation
1 parent 9290684 commit a9e3d58

File tree

1 file changed

+14
-20
lines changed

1 file changed

+14
-20
lines changed

examples/interactive_cartpole.m

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -98,28 +98,22 @@
9898
hold on;
9999
pole = plot(NaN, NaN, 'r-', 'LineWidth', 4);
100100

101-
% Initialize animation objects
102-
set(cart, 'XData', []);
103-
set(cart, 'YData', []);
104-
set(pole, 'XData', []);
105-
set(pole, 'YData', []);
106-
xlim([-2.5, 2.5]);
107-
ylim([-1, 1]);
108-
109101
% Run animation
110102
for frame = 1:Nsim
111-
x = x_all{frame};
112-
113-
% Update cart position
114-
set(cart, 'XData', x(1), 'YData', 0);
115-
116-
% Update pole position (length = 0.5)
117-
set(pole, 'XData', [x(1), x(1) - 0.5 * sin(x(3))], ...
118-
'YData', [0, 0.5 * cos(x(3))]);
119-
120-
drawnow;
121-
pause(0.01);
122-
F(frame) = getframe(gcf);
103+
if frame <= length(x_all)
104+
x = x_all{frame};
105+
106+
% Update cart position
107+
set(cart, 'XData', x(1), 'YData', 0);
108+
109+
% Update pole position (length = 0.5)
110+
set(pole, 'XData', [x(1), x(1) - 0.5 * sin(x(3))], ...
111+
'YData', [0, 0.5 * cos(x(3))]);
112+
113+
drawnow;
114+
pause(0.01);
115+
F(frame) = getframe(gcf);
116+
end
123117
end
124118

125119
%%

0 commit comments

Comments
 (0)