Why do I get an error: "Invalid or deleted object"

조회 수: 24 (최근 30일)
Denis Kartachov
Denis Kartachov 2018년 11월 30일
댓글: Denis Kartachov 2018년 12월 1일
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