Why do I get an error: "Invalid or deleted object"
24 views (last 30 days)
Show older comments
Denis Kartachov
on 30 Nov 2018
Commented: Denis Kartachov
on 1 Dec 2018
I'm running the following simulation in MatLab:
for i=1:length(t)
addpoints(curve1,posX(i),posY(i)); %trajectory in inertial frame
ball1 = viscircles([posX(i) posY(i)],r,'LineWidth',4,'Color','black');
framex=arrow([0 0],[R*cos(Theta(i)) R*sin(Theta(i))]);
framey=arrow([0 0],[-R*sin(Theta(i)) R*cos(Theta(i))]);
addpoints(curve2,X(i,1),X(i,2)); %trajectory in rotating frame
ball2 = viscircles([X(i,1) X(i,2)],r,'LineWidth',4,'Color','black');
if (posX(i))^2+(posY(i))^2 >= R^2
delete(ball1);delete(ball2);
break;
end
drawnow;
pause(0.001);
delete(ball1);delete(ball2);delete(framex);delete(framey);
if ishghandle(curve1) == 0
break;
end
end
I tried to download the "arrow" function so I can be able to plot and update reference frames in my simulation. When the simulation runs to completion, everything is fine. However if I close the figure before the loop is completed, I get the error "Invalid or deleted object". The error points to the line where I call
delete(ball1);delete(ball2);delete(framex);delete(framey);
So I pretty much know for a fact that this error arises from me deleting this arrow call in the loop, somehow it's deleting it before I close the figure, so it gives an error saying there's nothing to delete? I don't know why. I've been using the line function before just fine, but arrow gives an error. How come?
0 Comments
Accepted Answer
Image Analyst
on 1 Dec 2018
Edited: Image Analyst
on 1 Dec 2018
We can't run your code because of missing variables. I'd bet you're trying to delete something that doesn't exist, but don't really care. The easy way is to just wrap the line in try catch and ignore errors
try
delete(ball1);delete(ball2);delete(framex);delete(framey);
catch
end
Or you could use exist('ball1', 'var') to check for it in advance before deleting it.
if exist('ball1', 'var')
delete('ball1');
end
Finally, you could try
drawnow;
to make sure that the graphics are actually drawn and exist before deleting.
2 Comments
Walter Roberson
on 1 Dec 2018
You would test isvalid(ball1) or ishghandle(ball10 rather than whether ball1 exists as a variable.
More Answers (0)
See Also
Categories
Find more on Graphics Performance 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!