Main Content

Implementing S-Functions

MATLABS-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

一个2级马TLAB S-function consists of asetuproutine 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 atmsfuntmpl_basic.m.

The template consists of a top-levelsetupfunction and a set of skeleton local functions, each of which corresponds to a particular callback method. Eachcallback 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 thesetuproutine. SeeLevel-2 MATLAB S-Function Callback Methodsfor a table of the supported Level-2 MATLAB S-function callback methods.

A more detailed Level-2 MATLAB S-function template resides atmsfuntmpl.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. SeeWrite Level-2 MATLAB S-Functionsfor information on creating Level-2 MATLAB S-functions.

墨西哥人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 atsfuntmpl_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 seesfuntmpl_basic.c.

MEX VersusMATLABS-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.

SeeAvailable S-Function Implementationsfor information on choosing the type of S-function best suited for your application.

See Also

||||

Related Topics