Writing S-Functions | ![]() ![]() |
Example - Variable Sample Time S-Function
This M-file is an example of an S-function that uses a variable sample time. This example, in an M-file called vsfunc.m
, calls mdlGetTimeOfNextVarHit
when flag = 4
. Because the calculation of a next sample time depends on the input u
, this block has direct feedthrough. Generally, all blocks that use the input to calculate the next sample time (flag = 4
) require direct feedthrough. Here is the code for the M-file S-function.
function [sys,x0,str,ts] = vsfunc(t,x,u,flag) % This example S-function illustrates how to create a variable % step block in Simulink. This block implements a variable step % delay in which the first input is delayed by an amount of time % determined by the second input. % % dt = u(2) % y(t+dt) = u(t) % switch flag, case 0 [sys,x0,str,ts] = mdlInitializeSizes; % Initialization case 2 sys = mdlUpdate(t,x,u); % Update Discrete states case 3 sys = mdlOutputs(t,x,u); % Calculate outputs case 4 sys = mdlGetTimeOfNextVarHit(t,x,u); % Get next sample time case { 1, 9 } sys = []; % Unused flags otherwise error(['Unhandled flag = ',num2str(flag)]); % Error handling end % End of vsfunc. %============================================================== % mdlInitializeSizes % Return the sizes, initial conditions, and sample times for the % S-function. %============================================================== % function [sys,x0,str,ts] = mdlInitializeSizes % % Call simsizes for a sizes structure, fill it in and convert it % to a sizes array. % sizes = simsizes; sizes.NumContStates = 0; sizes.NumDiscStates = 1; sizes.NumOutputs = 1; sizes.NumInputs = 2; sizes.DirFeedthrough = 1; % flag=4 requires direct feedthrough % if input u is involved in % calculating the next sample time % hit. sizes.NumSampleTimes = 1; sys = simsizes(sizes); % % Initialize the initial conditions. % x0 = [0]; % % Set str to an empty matrix. % str = []; % % Initialize the array of sample times. % ts = [-2 0]; % variable sample time % End of mdlInitializeSizes. % %============================================================== % mdlUpdate % Handle discrete state updates, sample time hits, and major time % step requirements. %============================================================== % function sys = mdlUpdate(t,x,u) sys = u(1); % End of mdlUpdate. % %============================================================== % mdlOutputs % Return the block outputs. %============================================================== % function sys = mdlOutputs(t,x,u) sys = x(1); % end mdlOutputs % %============================================================== % mdlGetTimeOfNextVarHit % Return the time of the next hit for this block. Note that the % result is absolute time. %============================================================== % function sys = mdlGetTimeOfNextVarHit(t,x,u) sys = t + u(2); % End of mdlGetTimeOfNextVarHit.
mdlGetTimeOfNextVarHit
returns the time of the next hit, the time in the simulation when vsfunc
is next called. This means that there is no output from this S-function until the time of the next hit. In vsfunc
, the time of the next hit is set to t + u(2)
, which means that the second input, u(2)
, sets the time when the next call to vsfunc
occurs.
![]() | Example - Hybrid System S-Function | Writing S-Functions in C | ![]() |