Search code examples
matlabwindowobject-referencehandlesmatlab-app-designer

How to tell if a specific app is open and retrieve its window handle?


How can we detect that an app is opened and get a handle to its app object?

Before, with figure, it was practical to use some findobj on the figure 'Tag'. Then, we could check if it exists or not, and retrieve the content of the figure. But I didn't find how to manage it with an app.

Typically: I have a main app, that opens a new app window when I click on a button. Then, if I click on the button again, I want to check if the second app window is already opened or not, and if yes, take its app object, or delete the second window, etc. How can I capture this? Is there a way to capture it with findobj or similar function?

If I can avoid doing it by saving the app object with the get/setappdata, it would be nice.


Solution

  • To get a handle to the other window, I'd suggest creating a new property in your main app class that would store the 2nd app's handle, initialized to an empty graphic handle (gobjects(0)). Then, during the execution of the button callback, you check if this is a valid handle (~isempty(h) &&isvalid(h)), and based on the result either create a new uifigure or use the existing one.


    If you insist on obtaining a list of figure handles, then going over it and trying to find the right one based on known attributes such as the uifigure Name you can use this answer:

    hFigs = findall(groot, 'Type', 'figure');
    

    If distinguishing between figure/uifigure windows is required/helpful, you can also use this technique afterwards:

    hUIFigs = hFigs(arrayfun(@(x)isstruct(struct(x).ControllerInfo), hFigs));
    

    Alternatively, to tell if an app is open you could call the following undocumented function:

    webWindows = matlab.internal.webwindowmanager.instance.findAllWebwindows();
    

    then compare the 'Title' field of the returned objects to the one you're looking for.