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?

采纳的回答

Image Analyst
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
Walter Roberson 2018-12-1
You would test isvalid(ball1) or ishghandle(ball10 rather than whether ball1 exists as a variable.
Denis Kartachov
Denis Kartachov 2018-12-1
The first code you suggested worked perfectly, thank you very much

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Graphics Performance 的更多信息

产品


版本

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by