how to close all apps simultaneously?
Show older comments
Hi
I am closing one app and want the other one that calls the app I am closing, to close also.
How can I do that ? As a temporarily solution I have placed a dialog box asking the user whether she or he want to close the calling app also..
thanks
Answers (1)
Taylor
on 4 Dec 2023
Generally, you can just use "delete"
myHandle = myApp;
delete(myHandle)
I would recommend baking this into the "UIFigureCloseRequest" callback of your primary app like so
% this is called somewhere in the primary app
secondaryAppHandle = secondaryApp;
% this will go in UIFigureCloseRequest
delete(secondaryAppHandle);
7 Comments
Walter Roberson
on 4 Dec 2023
If you do not have the handles easily available, is there a way to find them?
Taylor
on 4 Dec 2023
myHandle = findall(0, "Type", "figure");
Walter Roberson
on 4 Dec 2023
No, that would close figure and uifigure, not apps. apps can contain zero or more graphics objects along with other properties.
Taylor
on 4 Dec 2023
Perhaps I'm misunderstanding your comment Walter, but I believe Apps are just a specific kind of figure/uifigure.

Calling
delete(myHandle)
in this case closes the App I have open. I also see all associated processes in Task Manager end after I call delete as well. Is there something I'm missing?
Walter Roberson
on 4 Dec 2023
The “matlab.apps.AppBase” class provides various features and functionality specific to App Designer, such as managing the app's components, handling events, and generating the user interface. It serves as the base class for your custom app class, allowing you to inherit and utilize the built-in functionality provided by App Designer.
MATLAB apps are objects that are derived from matlab.apps.AppBase . As objects they have properties and methods; the properties typically include a uifigure -- but the uifigure is not the app.
In the example you posted, that Figure had a Name field of 'MATLAB App' -- but that's just its Name, and it is not the app itself.
It is entirely possible that the default behaviour programmed into apps is that if the uifigure is closed, that the callbacks are responsible for deleting the app object -- but that behaviour is not inherent. If that callback behaviour were to get changed, then deleting the uifigure would not delete the app.
If what you have is handle to a uifigure, then there is no inherent property on the uifigure that refers to the app. If you poke around the callbacks of the objects contained in the uifigure you might be able to find a callback that contains a reference to the app, and might possibly be able to use functions on the anonymous function to look inside the anonymous function workspace to get the handle to the app. However, I am not currently aware of any straight-forward mechanism along the lines of
ancestor(HandleToUiFigure, 'app')
Taylor
on 4 Dec 2023
Ah ok I think I understand your point now, thank you for the explanation. When I call
delete(secondaryAppHandle);
I am relying on there being a "UIFigureCloseRequest" function in the secondary App that will properly close the secodary App not just the figure. So the user must make sure they have added this callback function to the secondary App. Deleting the figure object will trigger the UIFigureCloseRequest to be called which will stop the app.


Walter Roberson
on 4 Dec 2023
CloseRequestFcn handles calls to close() or clicking the window decorations to close the figure; CloseRequestFcn does not get invoked for delete() calls.
Categories
Find more on Develop Apps Using App Designer 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!