Writing S-Functions    

Chapter 2
Writing M S-Functions


Introduction

An M-file S-function consists of a MATLAB function of the following form

where f is the name of the S-function. During simulation of a model, Simulink repeatedly invokes f, using the flag argument to indicate the task (or tasks) to be performed for a particular invocation. Each time the S-function performs the task and returns the results in an output vector.

A template implementation of an M-file S-function, sfuntmpl.m, resides in matlabroot/toolbox/simulink/blocks. The template consists of a top-level function and a set of skeletal subfunctions, called S-function callback methods, each of which corresponds to a particular value of flag. The top-level function simply invokes the subfunction indicated by flag. The subfunctions perform the actual tasks required of the S-function during simulation.


 S-Function Examples S-Function Arguments