User`s guide
2 Writing S-Functions As M-Files
2-16
sizes.NumOutputs = 1;
sizes.NumInputs = 1;
sizes.DirFeedthrough = 0;
sizes.NumSampleTimes = 2;
sys = simsizes(sizes);
x0 = ones(2,1);
str = [];
ts = [0, 0 % sample time
dperiod, doffset];
% End of mdlInitializeSizes.
%
%==============================================================
% mdlDerivatives
% Compute derivatives for continuous states.
%==============================================================
%
function sys = mdlDerivatives(t,x,u)
sys = u;
% end of mdlDerivatives.
%
%==============================================================
% mdlUpdate
% Handle discrete state updates, sample time hits, and major time
% step requirements.
%==============================================================
%
function sys = mdlUpdate(t,x,u,dperiod,doffset)
% Next discrete state is output of the integrator.
% Return next discrete state if we have a sample hit within a
% tolerance of 1e-8. If we don't have a sample hit, return [] to
% indicate that the discrete state shouldn't change.
%
if abs(round((t-doffset)/dperiod)-(t-doffset)/dperiod) < 1e-8
sys = x(1); % mdlUpdate is "latching" the value of the
% continuous state, x(1), thus introducing a delay.
else
sys = []; % This is not a sample hit, so return an empty
end % matrix to indicate that the states have not
% changed.