Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Building Multi-Window Apps with the App Designer

Tech May 8 3

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:

  1. Sending data to the dialog: Write a StartupFcn callback 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.
  2. 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.
  3. Management tasks on close: Write a CloseRequestFcn callback 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.

  1. 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.
  2. 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 StartupFcn callback, 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
    
  3. 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.

  1. 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. The app argument (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
    
  2. 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 CallingApp as the first argument, followed by the data. Finally, call delete(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.

  1. Dialog app CloseRequestFcn:

    • Right-click app.UIFigure in 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
    
  2. Main app CloseRequestFcn:

    • Right-click app.UIFigure in 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
    

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.

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.