function invisible_figure_names = waitforallfiguresclosed()
% Copyright 2016-2020 The MathWorks, Inc.
% wait for all visible figures closed, then return the invisible figure names as cell array
% Wait for all visible figures are closed by the user
% Wait until all webwindows are closed by user
    % For numeric MCR, allchild and findobj is not on the Matlab path
    if (exist('allchild')~=2 || exist('findobj')~=5)
        invisible_figure_names = {};
        return;
    end
    flag= true;
    ww = 0;
    try
        ww = matlab.internal.webwindowmanager.instance;
    catch
    end    
    while (flag)
        visible_figures = findobj(allchild(groot),'flat','type','figure','visible','on');
        if (~isempty(visible_figures))
            waitfor(visible_figures(1));
        elseif (ww~=0 && ~isempty(ww.windowList))
            waitfor(ww.windowList(1));
        else
            flag = false;
        end
    end
    % Now get invisible figure names
    invisible_figures = findobj(allchild(groot),'flat','type','figure','visible','off');
    if (~isempty(invisible_figures))
        invisible_figure_names={invisible_figures.Name};
    else
        invisible_figure_names = {};
    end
end
