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:
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:
SourceSubsys
into the empty window.
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.
SourceSubsys
, as shown below.
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.
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 | ![]() |