MATLAB Answers

Info

This question is closed. Reopen it to edit or answer.

The code runs, but imediately deletes the animation and does not update

1 view (last 30 days)
Matthew Palermo
Matthew Palermo on 5 Dec 2019
Closed: MATLAB Answer Bot on 20 Aug 2021
I am modeling a knuckle-link linkage system with a crank-slider and I am trying to get this animated plot for velocity and to show the motion. The code runs and gives the inital positions of everything, but then it deletes itself and does not update the positions.
clear
clc
A = 8.25; %in
B = 48;
C = 42;
D = 20;
t = 0:0.01:20; %sec
omega = 5.0265; %rad/sec
theta = omega*t;
phi = cot(theta);
P1_pos = [56.25;43.1];
P2_pos = [56.25-A*cos(theta);43.1-A*sin(theta)];
P4_pos = C*[0;1];
P5_pos = [0*sin(theta);-D*cos(phi)]; %P5_pos = D*[0;-1+sin(theta)];
P3_pos = [sin(phi);cos(phi)]; %P3_pos = [C + B*cos(a+b); B*sin(a+b)];
P5x = P5_pos(1,:);
P5y = P5_pos(:,1);
P5vx = diff(P5x);
P5vy = diff(P5y);
P5v = sqrt(P5vx.^2 + P5vy.^2);
for i = length(t)
ani = subplot(2,1,1);
P1_pos_cir = viscircles(P1_pos',0.05);
P2_pos_cir = viscircles(P2_pos(:,i)',0.05);
P3_pos_cir = viscircles(P3_pos(:,i)',0.05); %P3_pos_cir = viscircles(P3_pos(:,i)',0.05);
P4_pos_cir = viscircles(P4_pos',0.05);
P5_pos_cir = viscircles(P5_pos(:,i)',0.05);
barA = line([P1_pos(1) P2_pos(1,i)],[P1_pos(2) P2_pos(2,i)]);
barB = line([P2_pos(1,i) P3_pos(1,i)],[P2_pos(2,i) P3_pos(2,i)]);
barC = line([P4_pos(1) P3_pos(1,i)],[P4_pos(2) P3_pos(2,i)]);
barD = line([P5_pos(1,i) P3_pos(1,i)],[P5_pos(2,i) P3_pos(2,i)]);
axis(ani,'equal');
set(gca,'XLim',[-10 60],'YLim',[-30 60]);
str1 = 'P5';
P5_text = text(P5_pos(1,i),P5_pos(2,i)+0.4,str1);
pause(0.001);
delete(P1_pos_cir);
delete(P2_pos_cir);
delete(P3_pos_cir);
delete(P4_pos_cir);
delete(P5_pos_cir);
delete(barA);
delete(barB);
delete(barC);
delete(barD);
delete(P5_text);
% vel = subplot(2,1,2);
% plot(vel,t(1:i),P5v(1:i));
% set(vel,'XLim',[0 50], 'YLim',[0 10]);
% xlabel(vel,'time(s)');
% ylabel(vel,'Velocity of Press Block');
% grid on;
end

Answers (1)

Jeremy
Jeremy on 5 Dec 2019
delete(P1_pos_cir);
delete(P2_pos_cir);
delete(P3_pos_cir);
delete(P4_pos_cir);
delete(P5_pos_cir);
delete(barA);
delete(barB);
delete(barC);
delete(barD);
delete(P5_text);
I think this needs to be moved outside the for loop

Tags

Community Treasure Hunt

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

Start Hunting!