Writing S-Functions | ![]() ![]() |
Creating Input and Output Ports
Simulink allows S-functions to create and use any number of block I/O ports. This section shows how to create and initialize I/O ports and how to change the characteristics of an S-function block's ports, such as dimensionality and data type, based on its connections to other blocks.
Creating Input Ports
To create and configure input ports, the mdlInitializeSizes
method should first specify the number of input ports that the S-function has, using ssSetNumInputPorts
. Then, for each input port, the method should specify:
mdlInitializeSizes
(see Sizing an Input Port Dynamically).
1=yes
or 0=no
. It should be set to 1 if the input, u
, is used in the mdlOutputs
or mdlGetTimeOfNextVarHit
routine. Setting the direct feedthrough flag to 0 tells Simulink that u
is not used in either of these S-function routines. Violating this leads to unpredictable results.
double
ssSetInputPortDataType
to set the input port's data type. If you want the data type of the port to depend on the data type of the port to which it is connected, specify the data type as DYNAMICALLY_TYPED
. In this case, you must provide implementations of the mdlSetInputPortDataType
and mdlSetDefaultPortDataTypes
methods to enable the data type to be set correctly during signal propagation.
ssSetInputComplexSignal
to set the input port's numeric type. If you want the numeric type of the port to depend on the numeric type of the port to which it is connected, specify the data type as inherited
. In this case, you must provide implementations of the mdlSetInputPortComplexSignal
and mdlSetDefaultPortComplexSignal
methods to enable the numeric type to be set correctly during signal propagation.
Initializing Input Port Dimensions
The following options exist for setting the input port dimensions:
w
, use
m
-by-n
, use
Sizing an Input Port Dynamically
If your S-function does not require that an input signal have a specific dimensionality, you might want to set the dimensionality of the input port to match the dimensionality of the signal connected to the port. To dimension an input port dynamically, your S-function should:
mdlInitializeSizes
.
to set the dimensionality of the input port.
If the input port can accept only vector (1-D) signals but the signals can be of any size, use
to specify the dimensionality of the input port.
If the input port can accept only matrix signals but can accept any row or column size, use
where m
and/or n
are DYNAMICALLY_SIZED
.
mdlSetInputPortDimensionInfo
method that sets the dimensions of the input port to the size of the signal connected to it.
![]() | Run-Time Parameters | Creating Output Ports | ![]() |