setup

Class: matlab.ui.componentcontainer.ComponentContainer
Package: matlab.ui.componentcontainer

Set up instance of component container subclass

Syntax

setup(obj)

Description

setup(obj) sets the initial state of the UI component. It executes once when the UI component object is created. Any property values passed as name-value pair arguments to the UI component's constructor method are assigned after the setup method executes.

Define this method to execute initialization code for each new instance of your class. For example, you can use this method to create the underlying graphics objects and set initial property values on those objects.

Input Arguments

expand all

Object of the class that inherits from the matlab.graphics.componentcontainer.ComponentContainer base class.

Attributes

Abstracttrue
Protectedtrue

To learn about attributes of methods, see Method Attributes.

Examples

expand all

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

To define the class, create a file called IPAddressComponent.m that contains the following class definition with these features:

  • A Value public property that stores the IP address.

  • NumericField and GridLayout private properties that place four numeric edit fields in a horizontal row.

  • A setup method that initializes NumericField and GridLayout.

  • An update method that updates the NumericField values when the IP address changes.

  • A handleNewValue method that sets the Value 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,5], ...
                'RowHeight',{22},'ColumnWidth',{30,30,30,30,22},...
                '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',@(o,e) 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 = IPAddressComponent;
 h.ValueChangedFcn = @(o,e) 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.

Introduced in R2020b