Products & Services Solutions Academia Support User Community Company

Learn more about Simulink   

How to Implement S-Functions

The following table gives an overview of how to write different types of S-functions. See the associated sections of the S-function documentation for more details on how to implement S-functions using a particular method.

S-Function TypeImplementation
Level-1 M-file S-functionUse the following template to write a new Level-1 M-file S-function:
sfuntmpl.m
See Maintaining Level-1 M-File S-Functions for more information.
Level-2 M-file S-function
  1. Use the following template to write a new Level-2 M-file S-function:

    msfuntmpl_basic.m

    See Writing Level-2 M-File S-Functions for more information.

  2. Write a Target Language Compiler (TLC) file for the S-function if you need to generate code for a model containing the S-function. The file, msfcn_times_two.tlcin the directory is an example TLC file for the S-function msfcn_times_two.m. See Inlining M-File S-Functions in Real-Time Workshop Target Language Compiler for information on writing TLC files for Level-2 M-file S-functions.

Hand-written C MEX S-function
  1. Use the following template to write a new C MEX S-function (see Example of a Basic C MEX S-Function) or to write a wrapper S-function that calls C, C++, or Fortran code:

    matlabroot/simulink/src/sfuntmpl_doc.c

    See Writing Wrapper S-Functions in the Real-Time Workshop User's Guide for information on writing wrapper S-functions to incorporate legacy C or C++ code. See Constructing the Gateway for information on writing a wrapper function to incorporate legacy Fortran code.

  2. Compile the S-function using the mex command to obtain an executable to use during simulation.

  3. Write a TLC file for the S-function if you want to inline the code during code generation (see Writing Fully Inlined S-Functions with the mdlRTW Routine in the Real-Time Workshop User's Guide and Real-Time Workshop Target Language Compiler) . You do not need a TLC file if you are not inlining the S-function in the generated code.

S-Function Builder
  1. Enter the S-function attributes into the S-Function Builder dialog box (see S-Function Builder Dialog Box).

  2. Select the Generate wrapper TLC option to generate a TLC file to inline the S-function during code generation.

  3. Click Build to generate the S-function, TLC file, and an executable file to use during simulation.

Legacy Code ToolUse the legacy_code function to perform the following steps (see Integrating Existing C Functions into Simulink Models with the Legacy Code Tool):
  1. Initialize a data structure that describes the S-function attributes in terms of the legacy function.

    lct_spec = legacy_code('initialize');
  2. Generate and compile the wrapper S-function.

    legacy_code('sfcn_cmex_generate', lct_spec);
    legacy_code('compile', lct_spec);
  3. Instantiate an S-Function block that calls the S-function wrapper.

    legacy_code('slblock_generate', lct_spec);
  4. Generate a TLC file to inline the S-function during code generation.

    legacy_code('sfcn_tlc_generate', lct_spec);

  


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