MATLAB: Delete a handles parameter

deleteguihandlesremove field from a structure

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

Best Answer

I don't think you can do that. You need to use rmfield() instead.
handles = rmfield(handles, 'CanvasPlot');