How close secondary apps when the main app closed?

I'm creating an app in matlab app designer with multiple secondary windows and the main window. I want to close all the secondary windows when the main window is ordered to be closed. I looked around on the forum and found some things about close the app. I tried to implement a FigureCloseRequest and delete(get(0, 'Children'), but didn't work.
So, I was planning to build a function that lists the sub-apps that are open and delete them when the main figure is requested to close, but I'm having trouble figuring out how to do it from the app propriety. Could you please aid me?

回答 (1 件)

Mohammad Sami
Mohammad Sami 2022 年 7 月 26 日

0 投票

You will need to store a reference to the secondary app in your main app. This will allow you to close it in the CloseRequestFcn of the main app. You can set this function in the main app by selecting UIFigure in the component browser in the design view. See attached picture.
Then assuming you have stored the handle to the secondary app in the main app you can do as follows in the Main App UIFigureCloseRequestFcn
function functionThatOpensSecondaryApp(app)
app.SecondaryApp = SecondaryApp(..);
end
function UIFigureCloseRequest(app, event)
delete(app.SecondaryApp);
delete(app);
end
More details can be found at the link below.

3 件のコメント

Nélio Dias
Nélio Dias 2022 年 7 月 26 日
The problem is that example is to simple. If I have two secondary apps, and only one is open, this code
function UIFigureCloseRequest(app, event)
delete(app.SecondaryApp1);
delete(app.SecondaryApp2);
delete(app);
end
will give me error because SecondaryApp2 is empty, so I need a way to check which app is open to delete it.
I tried to create a list of the apps open and delete them, but gives error too. This the code
methods (Access = public)
function createSubAppList(app, newSubApp)
app.mySubAppsList = [app.mySubAppsList, newSubApp];
end
function deleteSubApps(app)
delete(app.mySubAppsList)
end
end
% The event that create the subapp
function ButtonPushed(app, event)
app.SecondaryApp1 = SecondaryApp1(app.SecondaryApp1);
createSubAppList(app, app.SecondaryApp1);
end
% Close request function: LABVCONUIFigure
function UIFigureCloseRequest(app, event)
deleteSubApps(app)
delete(app)
end
end
Mohammad Sami
Mohammad Sami 2022 年 7 月 26 日
You can try the following
function deleteSubApps(app)
arrayfun(@delete,app.mySubAppsList);
end
Nélio Dias
Nélio Dias 2022 年 7 月 26 日
There is a problem, generating this error
The following error occurred converting from SecondaryApp2 to
SecondaryApp3:
Not enough input arguments.
I think when a create a new list the app try to call the secondary app again that is already in the list.

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeDevelop Apps Using App Designer についてさらに検索

製品

リリース

R2022a

質問済み:

2022 年 7 月 26 日

コメント済み:

2022 年 7 月 26 日

Community Treasure Hunt

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

Start Hunting!

Translated by