| Real-Time Workshop® | ![]() |
This section demonstrates how to extract a subsystem from a model and generate a reusable S-function component from it.
The next figure shows SourceModel, a simple model that inputs signals to a subsystem. The subsequent figure shows the subsystem, SourceSubsys. The signals, which have different widths and sample times, are
A Step block with sample time 1
A Sine Wave block with sample time 0.5
A Constant block whose value is the vector [-2 3]
SourceModel

SourceSubsys

The objective is to extract SourceSubsys from the model and build an S-Function block from it, using the S-function target. The S-Function block must perform identically to the subsystem from which it was generated.
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) hard-wired. (The sole exception to this rule concerns sample times, as described in Sample Time Propagation in Generated S-Functions.)
In this example, you want the S-Function block to retain the properties of SourceSubsys as it exists in SourceModel. Therefore, before you build the subsystem as a separate S-function component, you must set the inport sample times and widths 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,
Create a new model and copy/paste SourceSubsys into the empty window.
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.
The generated S-Function block should have three inports and one outport. Connect inports and an outport to SourceSubsys, as shown in the next figure.

The correct signal widths and sample times are propagated to these ports.
Set the solver type, mode, and other solver parameters such that they are identical to those of the source model. This is easiest to do if you use Model Explorer.
In Model Explorer or the Configuration Parameters dialog box, click the Real-Time Workshop tab.
In the System Target File Browser, select the S-function target, rtwsfcn.tlc, and click OK. The Real-Time Workshop pane appears as follows.

Select the Real-Time Workshop S-Function Code Generation Options tab (in Model Explorer) or pane (in the Configuration Parameters dialog box).
Make sure that Create New Model is selected, as shown in the next figure:

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.
Save the new model containing your subsystem, for example as SourceSubsys.mdl.
The Real-Time Workshop® build process builds the S-function component in the working directory. After the build, a new model window is displayed.

Optionally you can save the generated model, for example as SourceSubsys_Sfunction.mdl.
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.
Note For a list of files required to deploy your S-Function block for simulation or code generation, see Required Files for S-Function Deployment. |
The next figure shows the S-Function block plugged into the original model. Given identical input signals, the S-Function block will perform identically to the original subsystem.
Generated S-Function Configured Like SourceModel

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 | Tunable Parameters in Generated S-Functions | ![]() |
| © 1984-2008- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |