Main Content

Reuse Code Using Helper Functions

Helper functions are MATLAB® functions that you define in your app so that you can call them at different places in your code. For example, you might want to update a plot after the user changes a number in an edit field or selects an item in a drop-down list. Creating a helper function allows you to single-source the common commands and avoid having to maintain redundant code.

There are two types of helper functions: private functions, which you can call only inside your app, and public functions, which you can call either inside or outside your app. Private functions are commonly used in single-window apps, while public functions are commonly used in multiwindow apps.

Create a Helper Function

Code View provides a few different ways to create a helper function:

  • Expand the drop-down list from the bottom half of the Function button in the Editor tab. Select Private Function or Public Function.

    Function drop-down list with options "Private Function" and "Public Function"

  • Select the Functions tab in the Code Browser, expand the drop-down list on the Plus button, and select Private Function or Public Function.

    Plus button drop-down list with options "Private Function" and "Public Function"

When you make your selection, App Designer creates a template function and places your cursor in the body of that function. Then you can update the function name and its arguments, and add your code to the function body. The app argument is required, but you can add more arguments after the app argument. For example, this function creates a surface plot of the peaks function. It accepts an additional argument n for specifying the number of samples to display in the plot.

methods (Access = private)
    
        function updateplot(app,n)
            surf(app.UIAxes,peaks(n));
            colormap(app.UIAxes,winter);
        end

end

Call the function from within any callback. For example, this code calls the updateplot function and specifies 50 as the value for n.

updateplot(app,50);

Managing Helper Functions

Managing helper functions in the Code Browser is similar to managing callbacks. You can change the name of a helper function by double-clicking the name in the Functions tab of the Code Browser and typing a new name. App Designer automatically updates all references to the function when you change its name.

If your app has numerous helper functions, you can quickly search and navigate to a specific function by typing part of the name in the search bar at the top of the Functions tab. After you begin typing, the Functions tab clears, except for the items that match your search.

Functions tab of the Code Browser. The search bar contains the text "plot", and the list of functions below shows the function "updateplot(app,n)" with the text "plot" highlighted.

Click a search result to scroll the function into view. Right-clicking a search result and selecting Go To places your cursor in the function.

To delete a helper function, select its name in the Functions tab and press the Delete key.

Example: Helper Function That Initializes Plots and Displays Updated Data

This app shows how to create a helper function that initializes two plots and updates one of them in a component callback. The app calls the updateplot function at the end of the StartupFcn callback when the app starts up. The UITableDisplayDataChanged callback calls the same function to update one of the plots when the user sorts columns or changes a value in the table.

Related Topics