This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

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
        SampleTime = 1.4; % Sample Time
        OffsetTime = 0.2; % Offset Time
        TickTime = 0.1;
        SampleTimeTypeProp (1, 1) SampleTimeTypePropValues = SampleTimeTypePropValues.Discrete
    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',...
                case 'FixedInMinorStep'
                    sts = createSampleTime(obj,'Type','Fixed In Minor Step');
                case 'Discrete'
                    sts = createSampleTime(obj,'Type','Discrete',...
                      'SampleTime',obj.SampleTime, ...
                case 'Controllable'
                    sts = createSampleTime(obj,'Type','Controllable',...
        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')
            SampleTime = sts.SampleTime;
        function setupImpl(obj)
            obj.Count = 0;

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

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

See Also

| | | | |

Related Topics