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.
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.
function sts = getSampleTimeImpl(obj) switch obj.SampleTimeTypeProp case 'Inherited sample time' sts = createSampleTime(obj,'Type','Inherited'); case 'Inherited not controllable sample time' sts = createSampleTime(obj,'Type','Inherited',... 'Disallow','Controllable'); case 'Fixed In Minor Step sample time' sts = createSampleTime(obj,'Type','Fixed In Minor Step'); case 'Discrete sample time' sts = createSampleTime(obj,'Type','Discrete',... 'SampleTime',obj.SampleTime, ... 'OffsetTime',obj.OffsetTime); case 'Controllable sample time' sts = createSampleTime(obj,'Type','Controllable',... 'TickTime',obj.TickTime); end end
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.
function [y,ct,st] = stepImpl(obj,u) y = obj.Count + u; obj.Count = y; ct = getCurrentTime(obj); sts = getSampleTime(obj); if strcmp(sts.Type,'Controllable') setNumTicksUntilNextHit(obj,obj.Count); end st = sts.SampleTime; end
type CountTime.m
classdef CountTime < matlab.System & matlab.system.mixin.SampleTime % Counts Hits and Time properties(Nontunable) SampleTimeTypeProp = 'Discrete sample time'; % Sample Time Type SampleTime = 1.4; % Sample Time OffsetTime = 0.2; % Offset Time TickTime = 0.1; end properties(DiscreteState) Count end properties(Constant, Hidden) SampleTimeTypePropSet = matlab.system.StringSet(... {'Inherited sample time',... 'Inherited not controllable sample time',... 'Fixed In Minor Step sample time', ... 'Discrete sample time', ... 'Controllable sample time'}); end methods(Access = protected) function sts = getSampleTimeImpl(obj) switch obj.SampleTimeTypeProp case 'Inherited sample time' sts = createSampleTime(obj,'Type','Inherited'); case 'Inherited not controllable sample time' sts = createSampleTime(obj,'Type','Inherited',... 'Disallow','Controllable'); case 'Fixed In Minor Step sample time' sts = createSampleTime(obj,'Type','Fixed In Minor Step'); case 'Discrete sample time' sts = createSampleTime(obj,'Type','Discrete',... 'SampleTime',obj.SampleTime, ... 'OffsetTime',obj.OffsetTime); case 'Controllable sample time' 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 obj.SampleTimeTypeProp case {'Inherited sample time', ... 'Inherited not controllable sample time', ... 'Fixed In Minor Step sample time'} 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
Include this System object in a MATLAB System block.
model = 'CountTime_model';
open_system(model);
sim(model);
In the scope, you can see the effects of the sample time changes to the block.
open_system([model '/Scope']);
createSampleTime
| getCurrentTime
| getSampleTime
| getSampleTimeImpl
| matlab.system.mixin.SampleTime
| setNumTicksUntilNextHit