Main Content

Callbacks in App Designer

当用户执行的回调函数interacts with a UI component in your app. You can use callbacks to program the behavior of your app. For example, you can write a callback that plots some data when an app user clicks a button, or a callback that moves the needle of a gauge component when a user interacts with a slider.

Most components have at least one callback, and each callback is tied to a specific interaction with the component. However, some components, such as labels and lamps, do not have callbacks because those components only display information. To see the list of callbacks that a component supports, select the component and click theCallbackstab in theComponent Browser.

Create Callback Functions

有几种方法来创建一个回调UI component. You can take different approaches depending on where you are working in App Designer. Choose the most convenient approach from this list:

  • Right-click a component in the canvas,Component Browser, orApp Layoutpane, and selectCallbacks>Add (callback property) callback.

    Context menu associated with a slider component. Under the Callbacks menu item, there are options to add a ValueChangedFcn callback or a ValueChangingFcn callback.

  • Select theCallbackstab in theComponent Browser. The left side of theCallbackstab shows the supported callback properties. The drop-down list next to each callback property allows you to specify a name for the callback function or to select a default name in angle brackets <>. If your app has existing callbacks, the drop-down list includes those callbacks. Select an existing callback when you want multiple UI components to execute the same code.

    Callbacks tab of the Component Browser with a spinner component selected. There are options for a ValueChangedFcn callback and a ValueChangingFcn callback.

  • In codeCode View, in theEditortab, clickCallback. Alternatively, in theCode Browserpane, on theCallbackstab, click thePlusbutton.

    Add Callback Function dialog box. There are options to choose a component and a callback from drop-down lists, and to specify the callback function name.

    Specify these options in the Add Callback Function dialog box:

    • Component— Specify the UI component that executes the callback.

    • Callback— Specify the callback property. The callback property maps the callback function to a specific interaction. Some components have more than one callback property available. For example, sliders have two callback properties:ValueChangedFcnandValueChangingFcn. TheValueChangedFcncallback executes after the user moves the slider and releases the mouse. TheValueChangingFcncallback for the same component executes repeatedly while the user moves the slider.

    • Name— Specify a name for the callback function. App Designer provides a default name, but you can change it in the text field. If your app has existing callbacks, theNamefield has a drop-down arrow next to it, indicating that you can select an existing callback from a list.

Program Callback Functions

When you create a callback for a component, App Designer generates a callback function inCode Viewand places your cursor in the function. Write code in this callback function to program the callback behavior.

Callback Input Arguments

All callback functions that App Designer creates have these input arguments in the function signature:

  • app— Theappobject. Use this object to access UI components in the app as well as other variables stored as properties.

  • event— An object that contains specific information about the app user's interaction with the UI component.

Theappargument provides theappobject to your callback. You can access any component (and all component-specific properties) within any callback by using this syntax:

应用程序。Component.Property

For example, this command sets theValueproperty of a gauge to50. In this case, the name of the gauge isPressureGauge:

应用程序。PressureGauge.Value = 50;

Theeventargument provides an object that has different properties, depending on the specific callback that is executing. The object properties contain information that is relevant to the type of interaction that the callback is responding to. For example, theeventargument in aValueChangingFcncallback of a slider contains a property calledValue. That property stores the slider value as the user moves the thumb (before the user releases the mouse). Here is a slider callback function that uses theeventargument to make a gauge track the value of the slider:

functionSliderValueChanging(app,event) latestvalue = event.Value;% Current slider value应用程序。PressureGauge.Value = latestvalue;% Update gaugeend
To learn more about theeventargument for a specific component's callback function, see the property page for that component. Right-click the component, and selectHelp on Selectionto open the property page. For a list of property pages for all UI components, seeApp Building Components.

Share Data Between Callback Functions

To store data that needs to be accessed by multiple callbacks, create aproperty. Properties contain data that belongs to the app. You can createprivate propertiesto store data to be shared within the app only, orpublic propertiesto store data to be shared outside of the app (for example, with a script, function, or other app that needs access to the data).

Create a public or private property by clicking thePropertybutton in theEditortab inCode View. Enter a name for your property. You can then assign and access the property value within all of your app callbacks using the syntax应用程序。PropertyName.

For more information, seeShare Data Within App Designer Apps.

Share Callbacks Between Multiple Components

Sharing callbacks between components is useful when you want to offer multiple ways of doing something in your app. For example, your app can respond the same way when a user clicks a button or presses theEnterkey in an edit field.

After you create a callback for one component, you can share it by assigning it to a second component. Right-click the second component in theComponent Browserand selectCallbacks>Select existing callback. When the Select Callback Function dialog box displays, select the existing callback from theNamedrop-down list.

For an example of an app that shares a callback between two components, seeUse One Callback for Multiple App Designer Components.

Create and Assign Callbacks Programmatically

You can also create and assign callback functions programmatically in your app code. Use this method to create a callback for a component or graphics object that does not appear in theComponent Browser. For example, you can programmatically assign a callback to a dialog box that you create in your app code, or to aLineobject that you plot in aUIAxescomponent.

Create the callback function as a private function by selectingFunction>Private Functionin theEditortab of the toolstrip. The function must haveapp,src, andeventas the first three arguments. Here is an example of a callback written as a private function:

methods(Access = private)functionmyclosefcn(app,src,event) disp('Have a nice day!');endend

Assign the callback function to a component by specifying the callback property value as a handle to your callback function using the syntax@app.FunctionName. For example, this code creates an alert dialog box that assigns themyclosefcnfunction to the关闭Fcncallback property. The function executes when the dialog box closes.

uialert(app.UIFigure,"File not found","Alert",..."CloseFcn",@app.myclosefcn);

To write a callback function that accepts additional input arguments, specify the additional arguments after the first three arguments. For example, this callback accepts one additional input,name:

methods(Access = private)functiondisplaymsg(app,src,event,name) msg = name +" dialog box closed"; disp(msg);endend

To assign this callback to a component, specify the component callback property as cell array. The first element in the cell array must be the function handle. Subsequent elements must be the additional input values. For example:

uialert(app.UIFigure,"File not found","Alert",..."CloseFcn", {@app.displaymsg,"Alert"});

For more information, seeAdd UI Components to App Designer Programmatically.

Search for Callbacks in Your Code

If your app has a lot of callbacks, you can quickly search and navigate to a specific callback by typing part of the name in the search bar at the top of theCallbackstab in theCode Browser. After you begin typing, theCallbackspane clears, except for the callbacks that match your search.

Callbacks tab of the Code Browser. The search bar has the text

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

Change or Disconnect Callbacks

To assign a different callback to a component, select the component in theComponent Browser. Then click theCallbackstab and select a different callback from the drop-down list. The drop-down list displays only the existing callbacks.

Callbacks tab for an edit field component in the Component Browser. The ValueChangedFcn drop-down list is expanded, and two existing callback functions are listed.

To disconnect a callback that is shared with a component, select the component in theComponent Browser. Then click theCallbackstab and selectfrom the drop-down menu. Selecting this option only disconnects the callback from the component. It does not delete the function definition from your code, nor does it disconnect the callback from any other components. After you disconnect a callback, you can create a new callback for the component or leave the component without a callback function.

删除Callbacks

If your code contains a callback function that is not being used by any components in your app, you can delete the function entirely. Delete a callback by right-clicking the callback in theCallbackstab of theCode Browserand selecting删除from the context menu.

Callbacks tab of the Code Browser. The context menu associated with the StopButtonPushed callback function has options to delete, rename, insert at cursor, and go to.

Example: App with a Slider Callback

This app contains a gauge that tracks the value of a slider as the user moves the thumb. TheValueChangingFcncallback for the slider gets the current value of the slider from theeventargument. Then it moves the gauge needle to that value.

Related Topics