Building Multi-Window Apps with the App Designer
Multi-window applications consist of two or more separate apps that share data. A typical design involves a main app and a dialog box. The main app usually has a button that opens the dialog. When the dialog closes, it sends the user's selections back to the main app, which then performs calculations and updates its UI.
The two apps exchange information at differnet times and through different mechanisms:
- When the dialog opens, the main app calls the dialog app with input arguments, passing data to it.
- When the user clicks OK in the dialog, the dialog calls a public function in the main app using input arguments, returning the user's choices.
Process Overview
To create such a multi-window setup, you need to build two separate apps (main and dialog). The high-level tasks are:
- Sending data to the dialog: Write a
StartupFcncallback in the dialog app that accepts input arguments. One required argument is the main app object. Then, from the main app, call the dialog app with these arguments. - Returning data to the main app: Write a public function in the main app that updates its UI based on dialog choices. Since it is public, the dialog app can call it and pass values.
- Management tasks on close: Write a
CloseRequestFcncallback in each app to perform cleanup when the window closes.
Creating multiple app windows is not supported when deploying as a web app (requires MATLAB® Compiler™). In that case, consider a single-window app with multiple tabs.
Sending Data to the Dialog
Follow these steps to pass values from the main app to the dialog app.
-
Define input parameters for the dialog's StartupFcn:
- In the Code View of the dialog app, go to the Editor tab and click App Input Arguments.
- In the dialog box, enter a comma-separated list of variable names for input parameters. Specify:
- The main app object (so the dialog can access its functions and properties).
- Any other data the dialog needs.
- Click OK.
-
Store the main app object in the dialog app:
- First, add a private property to hold the main app reference. In Code View, select Properties > Private Properties. Change the property name to
CallingApp.
properties (Access = private) CallingApp % Main app end- Then, in the
StartupFcncallback, store the main app object in that property:
function StartupFcn(app, caller, sz, c) % Store main app object app.CallingApp = caller; % Process sz and c inputs as needed % ... end - First, add a private property to hold the main app reference. In Code View, select Properties > Private Properties. Change the property name to
-
Call the dialog from a callback in the main app:
- First, add a private property to store the dialog app object:
properties (Access = private) DialogApp % Dialog box app end- Then, add a callback (e.g., for an Options button) that disables the button, gathers data, and calls the dialog with input and output arguments. The output argument is the dialog app object.
function OptionsButtonPushed(app, event) % Disable button while dialog is open app.OptionsButton.Enable = "off"; % Get sample size and colormap values % ... % Open dialog with input values app.DialogApp = DialogAppExample(app, szvalue, cvalue); end
Returning Data to the Main App
Perform these steps to send user sleections from the dialog back to the main app.
-
Create a public function in the main app to update the UI:
- In Code View of the main app, from the Editor tab select Function > Public Function.
- Rename the function (e.g.,
updateplot) and add input parameters for each option from the dialog. Theappargument (main app object) must be first. Add code to process inputs and update the UI.
function updateplot(app, sz, c) % Process sz and c, update plot % ... end -
Call the public function from a callback in the dialog app:
- In Code View of the dialog app, add a callback for the OK button.
- Inside the callback, call the main app's public function. Pass the main app object stored in
CallingAppas the first argument, followed by the data. Finally, calldelete(app)to close the dialog.
function ButtonPushed(app, event) % Call main app's public function updateplot(app.CallingApp, app.EditField.Value, app.DropDown.Value); % Close the dialog delete(app) end
Managing Close Operations
Both apps need to handle cleanup when closed. Before the dialog closes, it must re-enable the Options button in the main app. Before the main app closes, it must ensure the dialog is closed.
-
Dialog app CloseRequestFcn:
- Right-click
app.UIFigurein the Component Browser and select Callbacks > Add CloseRequestFcn callback. - Add code to re-enable the button and close the dialog.
function DialogAppCloseRequest(app, event) % Re-enable the button in the main app app.CallingApp.OptionsButton.Enable = "on"; % Delete the dialog delete(app) end - Right-click
-
Main app CloseRequestFcn:
- Right-click
app.UIFigurein the Component Browser and select Callbacks > Add CloseRequestFcn callback. - Add code to close both apps.
function MainAppCloseRequest(app, event) % Delete both apps delete(app.DialogApp) delete(app) end - Right-click
Example
The following example consists of a plotting app with a button that opens a dialog for selecting plot options. The Options button calls the dialog app with input arguments. In the dialog, the callback for OK sends the user's choices back to the main app by calling a public function.