This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

Implementing S-Functions

MATLAB S-Functions

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

  • Multiple input and output ports

  • The ability to accept vector or matrix signals

  • Support for various signal attributes including data type, complexity, and signal frames

  • Ability to operate at multiple sample rates

A Level-2 MATLAB 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 msfuntmpl_basic.m.

The template consists of a top-level setup function and a set of skeleton local functions, 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 local functions using function handles defined in the setup routine. See Level-2 MATLAB S-Function Callback Methods for a table of the supported Level-2 MATLAB S-function callback methods.

A more detailed Level-2 MATLAB S-function template resides at msfuntmpl.m.

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

MEX S-Functions

Like a Level-2 MATLAB 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 MATLAB 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 S-function template resides at 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 sfuntmpl_basic.c.

MEX Versus MATLAB S-Functions

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

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

See Available S-Function Implementations for information on choosing the type of S-function best suited for your application.

Was this topic helpful?