IntroductionS-functions are short for system-functions. They are used for extending the capabilities of Simulink ® . S-functions allows us to add our own algorithms to Simulink models. The process of creating S-function blocks is quite simple. Simulink provides a S-function API which can be used to write a S-function routine observing a set of laid down rules. The compiled routine is enclosed inside a Simulink block, which can be subsequently customised by masking. A library of customised S-function blocks are created for an application specific task. This library can be subsequently distributed to work in MATLAB ® environment.
How is S-function useful?S-function can also be described as a computer language description of the Simulink block. S-function is written in any one of the popular languages viz C, C++, Fortran or Ada besides MATLAB's own M programming language and compiled as MEX files, where MEX stands for MATLAB Executable. S-functions use a special calling syntax called the S-function API that interacts with the Simulink engine. This interaction is very similar to the interaction that takes place between the engine and built-in Simulink blocks.In MATLAB S-functions, the S-function routines are implemented as MATLAB functions. In C MEX S-functions, they are implemented as C functions. All the S-function routines available to MATLAB S-functions exist for C MEX S-functions as well. However, Simulink provides a larger set of S-function routines for C MEX S-functions.S-function routines can be written for continuous, discrete or hybrid systems. A set of S-function blocks created by us can be placed in a tool box or library and distributed for working in MATLAB environment. S-functions allow creation of customised blocks for Simulink. By following a set of rules, any block algorithms can be implemented in an S-function. It can also be deployed for using an existing C code into a simulation. After compiling the S-function, the run time file has to be placed in an S-function block. User interface can then be customised by using masking. An advantage of using S-functions is that a general purpose block can be built that can be used many times in a model, varying parameters with each instance of the block.The most common usage of S-functions is for creating a set of custom Simulink blocks for an application. Existing C code in the application is easily encapsulated into S-function block and used as a separate Simulink block. This is used alongside the other blocks of Simulink.