Initialize Properties and Setup One-Time Calculations

This example shows how to write code to initialize and set up a System object™.

In this example, you initialize the internal state and count for a counter System object. You do these initialization tasks one time during setup, rather than every time you run the object.

Define Public Properties to Initialize

In this example, you define the public Threshold property and specify the value of that property. Users cannot change nontunable properties after the setup method has been called.

properties (Nontunable)
   Threshold = int32(1)
end

Define Private Properties to Initialize

Users cannot access private properties directly, but only through methods of the System object. In this example, you define State and Count as private properties.

properties (Access=private)
   State
   Count
end

Define Setup

You use the setupImpl method to perform setup and initialization tasks. You should include code in the setupImpl method that you want to execute one time only. The setupImpl method is called once the first time you run the object. In this example, you initialize the counter state and count value.

methods (Access=protected)
   function setupImpl(obj, ~)
      % Initialize states
      obj.Count = int32(0);
      obj.State = int32(0);
   end
end

Complete Class Definition File with Initialization and Setup

classdef CounterSysObj < matlab.System

    properties (Nontunable)
        Threshold = int32(1)
    end
    properties (Access=private)
        State
        Count
    end
    methods
        function obj = CounterSysObj(varargin)
            setProperties(obj,nargin,varargin{:});
        end
    end
    
    methods (Access=protected)
        function setupImpl(obj, ~)
            % Initialize states
            obj.Count = int32(0);
            obj.State = int32(0);
        end
        function y = stepImpl(obj, u)
            if obj.Threshold > u(1)
                obj.Count(:) = obj.Count + u(1); % Increment count
            end
            y = obj.State;          % Delay output
            obj.State = obj.Count;  % Put new value in state
        end
    end
end

See Also

|

Related Examples

More About

Was this topic helpful?