Specify Sample Time for MATLAB System Block System Objects

This example shows how to control the sample time of the MATLAB System block using System object™ methods.

Inside the class definition, use the matlab.system.mixin.SampleTime methods to configure the sample time and modify the System object behavior based on the current simulation time.

Specify Sample Time

To specify the sample time, implement the getSampleTimeImpl method. In this example, a property SampleTimeTypeProp is created to assign the sample time based on different property values. The getSampleTimeImpl method creates a sample time specification based on the SampleTimeTypeProp property. The getSampleTimeImpl method returns a sample time specification object sts to set the sample time specifications.

16            function sts = getSampleTimeImpl(obj)
17                switch char(obj.SampleTimeTypeProp)
18                    case 'Inherited'
19                        sts = createSampleTime(obj,'Type','Inherited');
20                    case 'InheritedNotControllable'
21                        sts = createSampleTime(obj,'Type','Inherited',...
22                            'Disallow','Controllable');
23                    case 'FixedInMinorStep'
24                        sts = createSampleTime(obj,'Type','Fixed In Minor Step');
25                    case 'Discrete'
26                        sts = createSampleTime(obj,'Type','Discrete',...
27                          'SampleTime',obj.SampleTime, ...
28                          'OffsetTime',obj.OffsetTime);
29                    case 'Controllable'
30                        sts = createSampleTime(obj,'Type','Controllable',...
31                            'TickTime',obj.TickTime);
32                end
33            end

Query Simulation Time and Sample Time

Use the getSampleTime and getCurrentTime methods to query the MATLAB System block for the current sample time and simulation time, respectively. getSampleTime returns a sample time specification object with properties describing the sample time settings.

35            function [Count, Time, SampleTime] = stepImpl(obj,u)
36               Count = obj.Count + u;
37                obj.Count = Count;
38                Time = getCurrentTime(obj);
39                sts = getSampleTime(obj);
40                if strcmp(sts.Type,'Controllable')
41                   setNumTicksUntilNextHit(obj,obj.Count);
42                end
43                SampleTime = sts.SampleTime;
44            end

Behavior in Simulink

Include this System object in a MATLAB System block.

In the scope, you can see the effects of the sample time changes to the block.

Full Class Definition

Full class definition of the CountTime System object and its associated enumeration class SampleTimeTypePropValues.

classdef CountTime < matlab.System & matlab.system.mixin.SampleTime
    % Counts Hits and Time
    
    properties(Nontunable)
        SampleTime = 1.4; % Sample Time
        OffsetTime = 0.2; % Offset Time
        TickTime = 0.1;
        SampleTimeTypeProp (1, 1) SampleTimeTypePropValues = SampleTimeTypePropValues.Discrete
    end
    
    properties(DiscreteState)
        Count
    end
    
    methods(Access = protected)
        function sts = getSampleTimeImpl(obj)
            switch char(obj.SampleTimeTypeProp)
                case 'Inherited'
                    sts = createSampleTime(obj,'Type','Inherited');
                case 'InheritedNotControllable'
                    sts = createSampleTime(obj,'Type','Inherited',...
                        'Disallow','Controllable');
                case 'FixedInMinorStep'
                    sts = createSampleTime(obj,'Type','Fixed In Minor Step');
                case 'Discrete'
                    sts = createSampleTime(obj,'Type','Discrete',...
                      'SampleTime',obj.SampleTime, ...
                      'OffsetTime',obj.OffsetTime);
                case 'Controllable'
                    sts = createSampleTime(obj,'Type','Controllable',...
                        'TickTime',obj.TickTime);
            end
        end
        
        function [Count, Time, SampleTime] = stepImpl(obj,u)
           Count = obj.Count + u;
            obj.Count = Count;
            Time = getCurrentTime(obj);
            sts = getSampleTime(obj);
            if strcmp(sts.Type,'Controllable')
               setNumTicksUntilNextHit(obj,obj.Count);
            end
            SampleTime = sts.SampleTime;
        end
        
        function setupImpl(obj)
            obj.Count = 0;
        end

        function resetImpl(obj)
            % Initialize / reset discrete-state properties
            obj.Count = 0;
        end
        
        function flag = isInactivePropertyImpl(obj,prop)
            flag = false;
            switch char(obj.SampleTimeTypeProp)
                case {'Inherited', ...
                        'InheritedNotControllable', ...
                        'FixedInMinorStep'}
                    if any(strcmp(prop,{'SampleTime','OffsetTime','TickTime'}))
                        flag = true;
                    end
                case 'Discrete'
                    if any(strcmp(prop,{'TickTime'}))
                        flag = true;
                    end
                case 'Controllable'
                    if any(strcmp(prop,{'SampleTime','OffsetTime'}))
                        flag = true;
                    end
            end
        end
    end
end

classdef SampleTimeTypePropValues < int32
    enumeration
        Discrete (0)
        Inherited (1)
        InheritedNotControllable (2)
        FixedInMinorStep (3)
        Controllable (4)
    end
end

See Also

| | | | |

Related Topics