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.

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