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.

Note

For backward compatibility, the following table and sections contain information about Level-1 MATLAB® S-functions. However, use the Level-2 MATLAB S-function API to develop new MATLAB S-functions.

S-Function TypeImplementation
Level-1 MATLAB S-functionUse the following template to write a new Level-1 MATLAB S-function: sfuntmpl.m
See Maintain Level-1 MATLAB S-Functions for more information.
Level-2 MATLAB S-function
  1. Use the msfuntmpl_basic.m template to write a new Level-2 MATLAB S-function:

    See Write Level-2 MATLAB 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.tlc in the folder is an example TLC file for the S-function msfcn_times_two.m. See Inline MATLAB File S-Functions (Simulink Coder) for information on writing TLC files for Level-2 MATLAB S-functions.

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

    For information on writing wrapper S-functions to incorporate legacy C or C++ code, see Write Wrapper S-Function and TLC Files (Simulink Coder). For information on writing a wrapper function to incorporate legacy Fortran code, see Constructing the Gateway.

  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 Write Fully Inlined S-Functions with mdlRTW Routine (Simulink Coder) and Target Language Compiler Basics (Simulink Coder)). 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 Integrate C Functions Using 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);