matlab.ui.componentcontainer.ComponentContainer.update - Update instance of UI component container subclass after setting
properties - MATLAB ([original](https://in.mathworks.com/help/matlab/ref/matlab.ui.componentcontainer.componentcontainer.update.html)) ([raw](?raw))
Main Content
Class: matlab.ui.componentcontainer.ComponentContainer
Namespace: matlab.ui.componentcontainer
Update instance of UI component container subclass after setting properties
Since R2020b
Description
update([obj](#mw%5Fd4b27eb4-4d8f-4e99-8abb-cfd8cd07a756%5Fsep%5Fmw%5Fe72055e4-681f-4c08-a285-ed570d7c1140))
updates the contents of the UI component after one or more 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 next drawnow
execution after the user changes one or more property values on the UI component.
Input Arguments
Object of the class that inherits from thematlab.ui.componentcontainer.ComponentContainer
base class.
Attributes
Abstract | true |
---|---|
Protected | true |
To learn about attributes of methods, seeMethod Attributes.
Examples
Define a class named IPAddressComponent
that creates a custom component for inputting four values to form an IP address.
To define the class, create a file named IPAddressComponent.m
that contains the following class definition with these features:
- A
Value
public property that stores the IP address. NumericField
andGridLayout
private properties that place four numeric edit fields in a horizontal row.- A
setup
method that initializesNumericField
andGridLayout
. - An
update
method that updates theNumericField
values when the IP address changes. - A
handleNewValue
method that sets theValue
property based on the values of the 4 numeric edit fields.
classdef IPAddressComponent < matlab.ui.componentcontainer.ComponentContainer % IPAddressComponent a set of 4 edit fields for IP Address input properties Value (1,4) {mustBeNonnegative, mustBeInteger, mustBeLessThanOrEqual(Value, 255)} = [192 168 1 2]; end
events (HasCallbackProperty, NotifyAccess = protected)
ValueChanged % ValueChangedFcn callback property will be generated
end
properties (Access = private, Transient, NonCopyable)
NumericField (1,4) matlab.ui.control.NumericEditField
GridLayout matlab.ui.container.GridLayout
end
methods (Access=protected)
function setup(obj)
% Set the initial position of this component
obj.Position = [100 100 150 22];
% Layout
obj.GridLayout = uigridlayout(obj,[1,4], ...
'RowHeight',{22},'ColumnWidth',{'1x','1x','1x','1x'},...
'Padding',0,'ColumnSpacing',2);
% Building blocks
for k = 1:4
obj.NumericField(k) = uieditfield(obj.GridLayout, 'numeric',...
'Limits', [0 255], 'RoundFractionalValues', true, ...
'FontName', 'Courier New', 'FontWeight', 'bold', ...
'ValueChangedFcn',@(src,event) obj.handleNewValue());
end
end
function update(obj)
% Update view
for k = 1:4
obj.NumericField(k).Value = obj.Value(k);
end
end
end
methods (Access=private)
function handleNewValue(obj)
obj.Value = [obj.NumericField.Value];
% Execute the event listeners and the ValueChangedFcn callback property
notify(obj,'ValueChanged');
end
end
end
Next, create the component by calling the IPAddressComponent
constructor method, which is provided by the ComponentContainer
class, and return the object as h
. Specify a function that displays the new IP address in the Command Window when the component value changes.
h.ValueChangedFcn = @(src,event) disp("Value changed to: " + num2str(h.Value));
Enter the IP Address 192.168.1.10
into the edit fields. MATLABĀ® displays the updated IP address in the Command Window.
Tips
- Do not call
drawnow
within thesetup
andupdate
methods of your UI component class. Such calls can cause unexpected screen updates in apps that use the UI component. Instead, rely on app creators (who use your component) to calldrawnow
in their app code when they need to trigger a screen update. These calls from outside the component code update all UI components in the app, including those created using theComponentContainer
class.
Version History
Introduced in R2020b