Difference between CloseRequestFcn and DeleteFcn?

47 visualizaciones (últimos 30 días)
M. A. Hopcroft
M. A. Hopcroft el 2 de En. de 2023
Comentada: M. A. Hopcroft el 4 de En. de 2023
Hello, I use the 'CloseRequectFcn' Figure property in my GUI design to hande "program shutdown"-related tasks, like closing files and windows, stopping cameras, etc. Recently, I took over a project where the previous author used 'DeleteFcn' for this purpose. My question is, what is the correct way to use these two functions? Is there a case where one is better than the other? Is one "more correct" for handling program exit? The documentation doesn't seem to give a clear answer. Thanks!
  5 comentarios
J. Alex Lee
J. Alex Lee el 3 de En. de 2023
Editada: J. Alex Lee el 3 de En. de 2023
apparently you don't even need to call delete inside the deleteFcn for the deletion to happen...
f = figure("DeleteFcn",@(o,e)disp("called delete"))
delete(f)
will still close the figure.
So I would say make your closerequestfcn just call your deletefcn callback, and do everything in there.
M. A. Hopcroft
M. A. Hopcroft el 4 de En. de 2023
I think that is the right approach. In general, in a user interface, the user can close a window using their local window manager (i.e. clicking the close button), but figure delete is a programmatic function that won't happen without some call to delete() to make it happen. So you need a CloseRequestFcn to do any user interaction that you want when the window is closed by the user. A DeleteFcn handles any cleanup tasks that do not need user interaction. So there is a reason to user one or the other or both. And, in both cases, if no callback Fcn is specified, the figure is deleted.

Iniciar sesión para comentar.

Respuesta aceptada

Rik
Rik el 3 de En. de 2023
Summarizing what has been mentioned in comments so far:
CloseRequestFcn
The CloseRequestFcn responds to any calls to the close function, as well as clicking on the close button of the figure window. That means that any calls inside it to close will result in a recursion. Without branching logic this means it will end up triggering a warning arising from infinite recursion.
This function does not close the figure, so you need to call delete explicitly to do so. This has the benefit of implementing an 'are you sure' message.
DeleteFcn
The DeleteFcn will trigger just prior to any call to delete, including those from the default CloseRequestFcn. There is no need to explicitly call delete in this function.
This function is most usefull to save any persistent state just prior to closing a GUI. There is no way to avoid the closing of the figure inside this function (expect perhaps via an error, I didn't test this).
  1 comentario
M. A. Hopcroft
M. A. Hopcroft el 3 de En. de 2023
The CloseRequestFcn does not close the figure, but it is not expected to, and same for DeleteFcn. The call to close() or delete() is what closes the window or figure.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Migrate GUIDE Apps en Help Center y File Exchange.

Productos


Versión

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by