Writing S-Functions    

Chapter 2
Writing M S-Functions


The following sections explain how to use the M programming language to create S-functions.

Introduction
Explains the syntax of an M S-function.
Defining S-Function Block Characteristics
How to specify the number of states, inputs and outputs, and other attributes of the block implemented by the M S-function.
Processing S-Function Parameters
How to process block parameters passed to the M S-function.
Examples of M-File S-Functions
Examples of M S-functions that implement various types of blocks.

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 skeleton subfunctions, called S-function callback methods, each of which corresponds to a particular value of flag. The top-level function 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