Error using matlab.graphics.chart.primitive.Line/set Invalid or deleted object.
18 views (last 30 days)
Show older comments
How do i deal with this problem? Attached below is a function that is supposed to "animate" and the script below that calls the function. Thanks.
%Function saved as AD_Crane
function crane(t,x)
w=0.75; %width of mass
h=0.75; %height of mass
l_r=0.75; %length of rod
w_r=0.1; %width of rod
x_mass = [-w/2,-w/2,w/2,w/2,-w/2]; %x coordinates of block
y_mass = [0,h,h,0,0]; %y coordinates of block
x_rod = [-w_r/2,-w_r/2,w_r/2,w_r/2,-w_r/2]; %x coordinates of rod
y_rod = [-l_r,0.1,0.1,-l_r,-l_r]; %y coordinates of rod
Hf=figure('Units','normalized','Position',[.1,.1,.8,.8]); %sets position of plot on screen
Ha_f2a1=subplot(2,1,1); %sets size of plot
Hls_f2plot1=plot(0,0);
axis([0,t(end),-2,2]);
grid on;
xlabel('t (sec)');
ylabel('x (m)') ;
Hp_f2mass=fill(x_mass+x(1),y_mass,'b');
axis([-2,2,-2,2]);
grid on;
Hl_m=line(x(1),h/2,'Marker','O','MarkerSize',8,'MarkerFaceColor','k'); %places dot at center of block
drawnow;
tic;
while toc<1
end;
tic;
for n=1:length(t)
%L=L0+x(n)-W/2;
set(Hls_f2plot1,'XDATA',t(1:n),'YDATA',x(1:n)); %attempts to plot as it cycles through values of t and x
set(Hp_f2mass,'YDATA',x_mass+x(n));
%set(Hl_cm,'XData',x(n));
%set(Hgt_springdamp,'Matrix',[L,0,0,-L0;0,1,0,H/2;0,0,1,0;0,0,0,1]);
drawnow;
end
%Script that calls above function
y0=[1 0 0 0];
tspan=[0:0.1:30];
[t,y]=ode45(@myodefun,tspan,y0,[]);
AD_Crane(t,y(:,3));
%plot(t,y)
function ydot= myodefun(t,y)
ydot(1,1)=y(2);
ydot(2,1)=(2.4*y(2)^2*sin(y(1)) + 9.81*sin(y(1))*cos(y(1)) + 0.125*(y(2)^2)*sin(y(1))*(cos(y(1))^2))/(0.5*cos(y(1)) + 0.25*cos(y(1))^2 -5.3);
ydot(3,1) = y(4);
ydot(4,1) = (-1.2625*y(2)^2*sin(y(1)) - 2.4525*sin(y(1))*cos(y(1)))/(-5.3 + 0.25*cos(y(1))^2);
end
0 Comments
Answers (1)
Cris LaPierre
on 3 Apr 2019
First, your function name and file name should be the same. I'd update funciton declaration to be AD_Crane(t,x)
%Function saved as AD_Crane
function crane(t,x)
Now, the issue is you replace the Hls_f2plot1 with the Hp_f2mass plot. So in your for loop at the end when you try to set properties of Hls_f2plot1, it no longer exists and you get the error. I suspect that you just forgot to assign Hp_f2mass_plot to subplot(2,1,2). Do that, and the error goes away.
Ha_f2a1=subplot(2,1,1); %sets size of plot
Hls_f2plot1=plot(0,0);
...
subplot(2,1,2)
Hp_f2mass=fill(x_mass+x(1),y_mass,'b');
0 Comments
See Also
Categories
Find more on Animation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!