Writing S-Functions | ![]() ![]() |
Scalar Expansion of Inputs
Scalar expansion of inputs refers conceptually to the process of expanding scalar input signals to have the same dimensions as the ports to which they are connected. This is done by setting each element of the expanded signal to the value of the scalar input. An S-function's mdlInitializeSizes
method can enable scalar expansion of inputs for its input ports by setting the SS_OPTION_ALLOW_INPUT_SCALAR_EXPANSION
option, using ssSetOptions
.
The best way to understand the scalar expansion rules is to consider a Sum
block with two input ports, where the first input signal is scalar, the second input signal is a 1-D vector with w > 1
elements, and the output signal is a 1-D vector with w
elements. In this case, the scalar input is expanded to a 1-D vector with w
elements in the output method, and each element of the expanded signal is set to the value of the scalar input.
Outputs <snip> u1inc = (u1width > 1); u2inc = (u2width > 1); for (i=0;i<w;i++) { y[i] = *u1 + *u2; u1 += u1inc; u2 += u2inc; }
If the block has more than two inputs, each input signal must be scalar, or the wide signals must have the same number of elements. In addition, if the wide inputs are driven by 1-D and 2-D vectors, the output is a 2-D vector signal, and the scalar inputs are expanded to a 2-D vector signal.
The way scalar expansion actually works depends on whether the S-function manages the dimensions of its input and output ports using mdlSetInputPortWidth and mdlSetOutputPortWidth or mdlSetInputPortDimensionInfo, mdlSetOutputPortDimensionInfo, and mdlSetDefaultPortDimensionInfo.
If the S-function does not specify/control the dimensions of its input and output ports using the preceding methods, Simulink uses a default method to set the input and output ports.
In the mdlInitializeSizes method, the S-function can enable scalar expansion for its input ports by setting the SS_OPTION_ALLOW_INPUT_SCALAR_EXPANSION
option, using ssSetOptions. The Simulink default method uses the preceding option to allow or disallow scalar expansion for a block's input ports. If the preceding option is not set by an S-function, Simulink assumes that all ports (input and output ports) must have the same dimensions, and it sets all port dimensions to the same dimensions specified by one of the driving blocks.
If the S-function specifies/controls the dimensions of its input and output ports, Simulink ignores the SCALAR_EXPANSION
option.
See matlabroot
/simulink/src/sfun_multiport.c
for an example.
![]() | Creating Output Ports | Masked Multiport S-Functions | ![]() |