Reputation: 599
I have a running script with many graphical handles that I update here and there in the code. My aim is that when I close the program brutaly (with X button or Ctrl C) to close it correctly.
I've overridden the default close function, yet when sometimes I still get "Invalid object handle" when pressing it or when pressing Ctrl+C.
I have many handles in the code, should check each one or is there another method of closing the figure and the code correctly? (Something like 'quit' but with out closing matlab IDE).
Thanks, Guy.
Upvotes: 2
Views: 123
Reputation: 4551
You can check the validity of a handle
with isvalid
. You should loop over your handles, check their validity, and close them if valid; you should probably also chuck a try...catch
in there for good measure. Something like:
function figure1_DeleteFcn(hObject, eventdata, handles)
errList = [];
for nHndl = 1:length(handles.myHandles)
if isvalid(handles.myHandles(nHndl))
try
delete(handles.myHandles(nHndl));
catch err
errList = [errList err];
end
end
end
if length(errList) == 1
error([mfilename ':ErrorClosingUI'], 'Error closing UI : %s', errList.message);
elseif length(errList) > 1
% Should probably do something smarter with the error messages here
error([mfilename ':ErrorClosingUI'], 'Multiple errors occurred closing UI');
end
Upvotes: 2