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:privatefunctions, which you can call only inside your app, andpublicfunctions, 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 menu from the bottom half of theFunctionbutton in theEditortab. SelectPrivate FunctionorPublic Function.

  • Select theFunctionstab in theCode Browser, expand the drop-down list on thebutton, and selectPrivate FunctionorPublic 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. Theappargument is required, but you can add more arguments after theappargument. For example, this function creates a surface plot of thepeaksfunction. It accepts an additional argumentnfor specifying the number of samples to display in the plot.

methods(Access = private)functionupdateplot(app,n) surf(app.UIAxes,peaks(n)); colormap(app.UIAxes,winter);endend

Call the function from within any callback. For example, this code calls theupdateplotfunction and specifies50as the value forn.

updateplot(app,50);

Managing Helper Functions

Managing helper functions in theCode Browseris similar to managing callbacks. You can change the name of a helper function by double-clicking the name in theFunctionstab of theCode Browserand 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 theFunctionstab. After you begin typing, theFunctionstab clears, except for the items that match your search.

Click a search result to scroll the function into view. Right-clicking a search result and selectingGo Toplaces your cursor in the function.

To delete a helper function, select its name in theFunctionstab and press the删除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 theupdateplotfunction at the end of theStartupFcncallback when the app starts up. TheUITableDisplayDataChangedcallback calls the same function to update one of the plots when the user sorts columns or changes a value in the table.

Related Topics