Real-Time Workshop    

Creating an S-Function Block from a Subsystem

This section demonstrates how to extract a subsystem from a model and generate a reusable S-function component from it.

Figure 10-1 illustrates SourceModel, a simple model that inputs signals to a subsystem. Figure 10-2 illustrates the subsystem, SourceSubsys. The signals, which have different widths and sample times, are:

Figure 10-1: SourceModel

Figure 10-2: SourceSubsys

Our objective is to extract SourceSubsys from the model and build an S-Function block from it, using the S-function target. We want the S-Function block to perform identically to the subsystem from which it was generated.

Note that in this model, SourceSubsys inherits sample times and signal widths from its input signals. However, S-function blocks created from a model using the S-function target will have all signal attributes (such as signal widths or sample times) hardwired. (The sole exception to this rule concerns samples times, as described in Sample Time Propagation in Generated S-Functions.)

In this example, we want the S-Function block to retain the properties of SourceSubsys as it exists in SourceModel. Therefore, before building the subsystem as a separate S-function component, the inport sample times and widths must be set explicitly. In addition, the solver parameters of the S-function component must be the same as those of the original model. This ensures that the generated S-function component will operate identically to the original subsystem (see Choice of Solver Type for an exception to this rule).

To build SourceSubsys as an S-function component:

  1. Create a new model and copy/paste SourceSubsys into the empty window.
  2. Set the signal widths and sample times of inports inside SourceSubsys such that they match those of the signals in the original model. Inport 1, Filter, has a width of 1 and a sample time of 1. Inport 2, Xferfcn, has a width of 1 and a sample time of 0.5. Inport 3, offsets, has a width of 2 and a sample time of 0.5.
  3. The generated S-Function block should have three inports and one outport. Connect inports and an outport to SourceSubsys, as shown below.
  1. Note that the correct signal widths and sample times propagate to these ports.

  1. Set the solver type, mode, and other solver parameters such that they are identical to those of the source model.
  2. Save the new model.
  3. Open the Simulation Parameters dialog and click the Real-Time Workshop tab. On the Real-Time-Workshop pane, select Target configuration from the Category menu.
  4. Click the Browse button to open the System Target Browser. Select the S-function target in the System Target Browser, and click OK. The Real-Time-Workshop pane parameters should appear as below.

  5. Select RTW S-function code generation options from the Category menu. Make sure that Create New Model is selected.
  1. When this option is selected, the build process creates a new model after it builds the S-function component. The new model contains an S-Function block, linked to the S-function component.

  1. Click Apply if necessary.
  2. Click Build.
  3. Real-Time Workshop builds the S-function component in the working directory. After the build, a new model window displays.

  1. You can now copy the Real-Time Workshop S-Function block from the new model and use it in other models or in a library. Figure 10-3 shows the S-Function block plugged in to the original model. Given identical input signals, the S-Function block will perform identically to the original subsystem.

Figure 10-3: Generated S-Function Plugged into SourceModel

Note that the speed at which the S-Function block executes is typically faster than the original model. This difference in speed is more pronounced for larger and more complicated models. By using generated S-functions, you can increase the efficiency of your modeling process.


  Introduction Sample Time Propagation in Generated S-Functions