Products & Services Solutions Academia Support User Community Company

Learn more about Simulink   

Implementing S-Functions

M-File S-Functions

Level-2 M-file S-functions allow you to create blocks with many of the features and capabilities of Simulink built-in blocks, including:

A Level-2 M-file S-function consists of a setup routine to configure the basic properties of the S-function, and a number of callback methods that the Simulink engine invokes at appropriate times during the simulation.

A basic annotated version of the template resides at

matlabroot/toolbox/simulink/blocks/msfuntmpl_basic.m

The template consists of a top-level setup function and a set of skeleton subfunctions, each of which corresponds to a particular callback method. Each callback method performs a specific S-function task at a particular point in the simulation. The engine invokes the subfunctions using function handles defined in the setup routine. See Level-2 M-File S-Function Callback Methods for a table of the supported Level-2 M-file S-function callback methods.

A more detailed Level-2 M-file S-function template resides at

matlabroot/toolbox/simulink/blocks/msfuntmpl.m

We recommend that you follow the structure and naming conventions of the templates when creating Level-2 M-file S-functions. This makes it easier for others to understand and maintain the M-file S-functions that you create. See Writing S-Functions in M for information on creating Level-2 M-file S-functions.

MEX-File S-Functions

Like a Level-2 M-file S-function, a MEX S-function consists of a set of callback methods that the Simulink engine invokes to perform various block-related tasks during a simulation. MEX S-functions can be implemented in C, C++, or Fortran. The engine directly invokes MEX S-function routines instead of using function handles as with M-file S-functions. Because the engine invokes the functions directly, MEX S-functions must follow standard naming conventions specified by the S-function API.

An annotated C MEX-file S-function template resides at

matlabroot/simulink/src/sfuntmpl_doc.c

The template contains skeleton implementations of all the required and optional callback methods that a C MEX S-function can implement.

For a more basic version of the template see

matlabroot/simulink/src/sfuntmpl_basic.c

MEX-File Versus M-File S-Functions

Level-2 M-file and MEX-file S-functions each have advantages. The advantage of Level-2 M-file S-functions is speed of development. Developing Level-2 M-file S-functions avoids the time consuming compile-link-execute cycle required when developing in a compiled language. Level-2 M-file S-functions also have easier access to MATLAB toolbox functions and can utilize the MATLAB Editor/Debugger.

MEX-file S-functions are more appropriate for integrating legacy code into a Simulink model. For more complicated systems, MEX-file S-functions may simulate faster than M-file S-functions because the Level-2 M-file S-function calls the MATLAB interpreter for every callback method.

See Selecting an S-Function Implementation for information on choosing the type of S-function best suited for your application.

  


Related Products & Applications

Learn more about Simulink through this collection of videos, articles, technical literature and the Getting Started with Simulink Guide.

 © 1984-2009- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS