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 System object sample time methods to configure the sample time and modify the System object behavior based on the current simulation time. If you want to use inherited sample time, you do not need to specify a sample time in your System object definition.
Specify Sample Time
To specify the sample time, implement the getSampleTimeImpl method and create a sample time specification object with createSampleTime.
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 the sample time specification object sts created by createSampleTime.
18 methods(Access = protected) 19 function sts = getSampleTimeImpl(obj) 20 switch char(obj.SampleTimeTypeProp) 21 case 'Inherited' 22 sts = createSampleTime(obj,'Type','Inherited'); 23 case 'InheritedNotControllable' 24 sts = createSampleTime(obj,'Type','Inherited',... 25 'AlternatePropagation','Controllable'); 26 case 'InheritedErrorConstant' 27 sts = createSampleTime(obj,'Type','Inherited',... 28 'ErrorOnPropagation','Constant'); 29 case 'FixedInMinorStep' 30 sts = createSampleTime(obj,'Type','Fixed In Minor Step'); 31 case 'Discrete' 32 sts = createSampleTime(obj,'Type','Discrete',... 33 'SampleTime',obj.SampleTime, ... 34 'OffsetTime',obj.OffsetTime); 35 case 'Controllable' 36 sts = createSampleTime(obj,'Type','Controllable',... 37 'TickTime',obj.TickTime); 38 end 39 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.
40 41 function [Count, Time, SampleTime] = stepImpl(obj,u) 42 Count = obj.Count + u; 43 obj.Count = Count; 44 Time = getCurrentTime(obj); 45 sts = getSampleTime(obj); 46 if strcmp(sts.Type,'Controllable') 47 setNumTicksUntilNextHit(obj,obj.Count); 48 end 49 SampleTime = sts.SampleTime; 50 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.
classdef CountTime < matlab.System % Counts Hits and Time properties(Nontunable) SampleTime = 1.4; % Sample Time OffsetTime = 0.2; % Offset Time TickTime = 0.1; SampleTimeTypeProp (1,:) char {mustBeMember(SampleTimeTypeProp, ... {'Discrete','FixedInMinorStep','Controllable',... 'Inherited','InheritedNotControllable',... 'InheritedErrorConstant'})} = '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',... 'AlternatePropagation','Controllable'); case 'InheritedErrorConstant' sts = createSampleTime(obj,'Type','Inherited',... 'ErrorOnPropagation','Constant'); 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