- Calling 'delete' as you are (and as Sean de Wolski is explaining syntax for below) is done on a graphics handle(s) (or a handle-derived object in OOP) and will delete the graphics object. However it will leave the field still there on handles pointing to a deleted graphics object
- Using rmfield as Image Analyst shows below is the second part of that which will remove the field from handles that now points to a deleted graphics object.
Delete a handles parameter
13 views (last 30 days)
Show older comments
I have a set of subplots that I plot on the canvas. I collectively assign to the handles structure using
handles.CanvasPlot=hSubplots; %assign hSubplots to a handle called CanvasPlot
guidata(hObject, handles); % Update handle structure
and then delete when its required as:
if isfield(handles,'CanvasPlot')
delete(handles.CanvasPlot);
end
But it appears to delete this when its not present, i.e. when I first load code, there is no hsubplots and hence CanvasPlot, I thought the check above would catch this. I get the error
Error using delete
Invalid or deleted object.
Error in Montage>pushbutton1_Callback (line 124)
delete(handles.CanvasPlot);
Im not sure whats happening. Thanks Jason
1 Comment
Adam
on 23 Sep 2014
Edited: Adam
on 23 Sep 2014
Just to add a comment to tie together the two answers below which both explain their part of the functionality well. You may know all this anyway, but in case someone else looks at your question later on hoping to solve a similar problem:
There are two different aspects going on here and I assume you want both parts.
Personally in the latter case I tend to just set the field to [] and test using isempty at other points in my code (having created the field in the GUI's OpeningFcn) rather than removing the field ans testing with isfield, but both methods are equally valid.
One advantage to using the [] option though is that you can call the equivalent of
delete( [] )
without a syntax error. However, what you can't do is call
delete( someGUIHandle )
if 'someGUIHandle' is neither empty nor a valid handle (i.e. you have deleted it as you do above) so you do usually still need to do the ishandle( someGUIHandle ) checks before calling delete to be sure.
Accepted Answer
Image Analyst
on 23 Sep 2014
I don't think you can do that. You need to use rmfield() instead.
handles = rmfield(handles, 'CanvasPlot');
2 Comments
More Answers (1)
Sean de Wolski
on 23 Sep 2014
dbstop if error
Will stop in debug mode when the error is thrown. You can see what it's doing here. Another thing you could do is check that they are handles:
if isfield(handles,'CanvasPlot') && ishandle(handles.CanvasPlot)
delete(handles.CanvasPlot);
end
10 Comments
See Also
Categories
Find more on Interactive Control and Callbacks 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!