Writing S-Functions    

What Is an S-Function?

An S-function is a computer language description of a Simulink block. S-functions can be written in MATLAB, C, C++, Ada, or Fortran. C, C++, Ada, and Fortran S-functions are compiled as MEX-files using the mex utility (see Building MEX-Files in the online MATLAB documentation). As with other MEX-files, they are dynamically linked into MATLAB when needed.

S-functions use a special calling syntax that enables you to interact with Simulink's equation solvers. This interaction is very similar to the interaction that takes place between the solvers and built-in Simulink blocks. The form of an S-function is very general and can accommodate continuous, discrete, and hybrid systems.

S-functions allow you to add your own blocks to Simulink models. You can create your blocks in MATLAB®, C, C++, Fortran, or Ada. By following a set of simple rules, you can implement your algorithms in an S-function. After you write your S-function and place its name in an S-Function block (available in the Functions & Tables block library), you can customize the user interface by using masking.

You can use S-functions with the Real-Time Workshop®. You can also customize the code generated by the Real Time Workshop for S-functions by writing a Target Language CompilerTM (TLC) file. See Writing S-Functions for Real-Time Workshop and the Real-Time Workshop documentation for more information.


  Overview of S-Functions Using S-Functions in Models