Main Content

update

更新UI组件容器子类的实例after setting properties

Syntax

update(obj)

Description

update(obj)updates the contents of the UI component after one or more public property values change. Define this method to update the underlying graphics objects in the UI component using the new property values. This method executes during the nextdrawnowexecution after the user changes one or more property values on the UI component.

Input Arguments

expand all

Object of the class that inherits from thematlab.ui.componentcontainer.ComponentContainerbase class.

Attributes

Abstract true
Protected true

To learn about attributes of methods, seeMethod Attributes

Examples

expand all

Define a class calledIPAddressComponentthat creates a custom component for inputting four values to form an IP address.

To define the class, create a file calledIPAddressComponent.mthat contains the following class definition with these features:

  • AValuepublic property that stores the IP address.

  • NumericFieldandGridLayoutprivate properties that place four numeric edit fields in a horizontal row.

  • Asetupmethod that initializesNumericFieldandGridLayout

  • Anupdatemethod that updates theNumericFieldvalues when the IP address changes.

  • AhandleNewValuemethod that sets theValueproperty based on the values of the 4 numeric edit fields.

classdefIPAddressComponent < matlab.ui.componentcontainer.ComponentContainer% IPAddressComponent a set of 4 edit fields for IP Address inputpropertiesValue(1,4) {mustBeNonnegative, mustBeInteger, mustBeLessThanOrEqual(Value, 255)}= [192 168 1 2];endevents(HasCallbackProperty, NotifyAccess = protected) ValueChanged% ValueChangedFcn callback property will be generatedendproperties(Access = private, Transient, NonCopyable) NumericField(1,4) matlab.ui.control.NumericEditFieldGridLayoutmatlab.ui.container.GridLayoutendmethods(Access=protected)functionsetup(obj)% Set the initial position of this componentobj.Position = [100 100 150 22];% Layoutobj.GridLayout = uigridlayout(obj,[1,5],。..'RowHeight',{22},'ColumnWidth',{30,30,30,30,22},。..'Padding',0,'ColumnSpacing',2);% Building blocksfork = 1:4 obj.NumericField(k) = uieditfield(obj.GridLayout,'numeric',。..'Limits', [0 255],'RoundFractionalValues', true,。..'FontName','Courier New','FontWeight','bold',。..'ValueChangedFcn',@(o,e) obj.handleNewValue());endendfunctionupdate(obj)% Update viewfork = 1:4 obj.NumericField(k).Value = obj.Value(k);endendendmethods(Access=private)functionhandleNewValue(obj) obj.Value = [obj.NumericField.Value];% Execute the event listeners and the ValueChangedFcn callback propertynotify(obj,'ValueChanged');endendend

Next, create the component by calling theIPAddressComponentconstructor method, which is provided by theComponentContainerclass, and return the object ash。指定一个函数显示新的IP address in the Command Window when the component value changes.

h = IPAddressComponent; h.ValueChangedFcn = @(o,e) disp(['Value changed to: ', num2str(h.Value)]);

Enter the IP Address192.168.1.10into the edit fields. MATLAB displays the updated IP address in the Command Window.

Version History

Introduced in R2020b