Delete a handles parameter

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 comentario

Adam
Adam el 23 de Sept. de 2014
Editada: Adam el 23 de Sept. de 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.
  • 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.
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.

Iniciar sesión para comentar.

 Respuesta aceptada

Image Analyst
Image Analyst el 23 de Sept. de 2014

1 voto

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

2 comentarios

Jason
Jason el 23 de Sept. de 2014
Thankyou, this works:
if isfield(handles,'CanvasPlot')
handles = rmfield(handles, 'CanvasPlot');
end
Chandan Prakash
Chandan Prakash el 1 de Jul. de 2020
Thank you. This works perfect.

Iniciar sesión para comentar.

Más respuestas (1)

Sean de Wolski
Sean de Wolski el 23 de Sept. de 2014

0 votos

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 comentarios

Jason
Jason el 23 de Sept. de 2014
Thanks Sean, I tried but got this error:
Operands to the || and && operators must be convertible to logical scalar values.
Error in Montage>pushbutton1_Callback (line 124)
if (isfield(handles,'CanvasPlot')&& ishandle(handles.CanvasPlot))
Sean de Wolski
Sean de Wolski el 23 de Sept. de 2014
Probably:
all(ishandle(handles.CanvasPlot))
Since there are multiple. But depending on what you want to do this might not be right anyway. My code deletes the subplot axes; IA's removes the field from the structure - two different operations.
Image Analyst
Image Analyst el 23 de Sept. de 2014
It would delete the axes if you had assigned handles.CanvasPlot to some variable and deleted that variable instead of handles.CanvasPlot. Look at the following simple code - it fails when you try to delete one of the fields.
h.a = 1;
h.b = 2
delete(h.a) % Fails
Sean de Wolski
Sean de Wolski el 24 de Sept. de 2014
Editada: Sean de Wolski el 24 de Sept. de 2014
But not if figure 1 is open:
h.a = 1;
figure(1)
pause(1)
h.b = 2
delete(h.a) % woohoo
Jason
Jason el 25 de Sept. de 2014
Is it possible to draw draw my subplots onto a uipanel, and then just clear this?
Adam
Adam el 25 de Sept. de 2014
Editada: Adam el 25 de Sept. de 2014
You could quite easily get all children of the panel, check if they are axes and if they are call 'cla' on them.
As discussed above though, this will, of course, still leave you with deleted handle references if you stored the handles of the plots in your handles.CanvasPlot field.
Jason
Jason el 25 de Sept. de 2014
So Ive tried this:
Draw the plots to the uipanel,
hp = handles.uipanelM
h=subplot('Position',positionVector,'Parent',hp);
Then to clear (as wanting to draw again later)
cla(handles.uipanelM,'reset');
Image Analyst
Image Analyst el 25 de Sept. de 2014
Did that work? Did all the plots show up arrayed inside the panel rather than on the main figure?
Jason
Jason el 25 de Sept. de 2014
looks like this works:
delete(get(handles.uipanelM,'Children'))
Jason
Jason el 25 de Sept. de 2014
Yes IA, all the images did array (or montage), just all a bit smaller, so I can play with that part. the delete worked too using this way:
delete(get(handles.uipanelM,'Children'))

Iniciar sesión para comentar.

Categorías

Más información sobre Interactive Control and Callbacks en Centro de ayuda y File Exchange.

Preguntada:

el 23 de Sept. de 2014

Comentada:

el 1 de Jul. de 2020

Community Treasure Hunt

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

Start Hunting!

Translated by