Why do I get an error: "Invalid or deleted object"
24 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Image Analyst
2018-12-1
编辑:Image Analyst
2018-12-1
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 个评论
Walter Roberson
2018-12-1
You would test isvalid(ball1) or ishghandle(ball10 rather than whether ball1 exists as a variable.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Graphics Performance 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!