Main Content

Create Input and Output Ports

Creating Input Ports for Level-2 MATLAB S-Functions

To create and configure input ports, the setup method should first specify the number of S-function input ports, using the run-time object NumInputPorts property. Next, if all input ports inherit their functional properties (data type, dimensions, complexity, and sampling mode) from their input signals, include the following line in the setup method:

block.SetPreCompInpPortInfoToDynamic;

Then, for each input port, the setup method can specify

  • The dimensions of the input port, using block.InputPort(n).Dimensions.

    To individually specify that an input port's dimensions are dynamically sized, assign a value of -1 to the dimensions. In this case, you can implement the SetInputPortDimensions method to set the dimensions during signal propagation.

  • Whether the input port has direct feedthrough, using block.InputPort(n).DirectFeedthrough.

    A port has direct feedthrough if the input is used in the Outputs functions to calculate either the outputs or the next sample time hit. The direct feedthrough flag for each input port can be set to either 1=yes or 0=no. Setting the direct feedthrough flag to 0 tells the Simulink® engine that u is not used to calculate the outputs or next sample time hit. Violating this leads to unpredictable results.

  • The data type of the input port, using block.InputPort(n).DatatypeID. See the explanation for the DatatypeID property in the Simulink.BlockData data object reference page for a list of valid data type IDs.

    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 -1. In this case, you can implement the SetInputPortDataType method to set the data type during signal propagation.

  • The numeric type of the input port, if the port accepts complex-valued signals, using block.InputPort(n).Complexity.

    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 numeric type as 'Inherited'. In this case, you can implement the SetInputPortComplexSignal method to set the numeric type during signal propagation.

Creating Output Ports for Level-2 MATLAB S-Functions

To create output ports for Level-2 MATLAB® S-functions the setup method should first specify the number of S-function output ports, using the run-time object NumOutputPorts property. Next, if all output ports inherit their functional properties (data type, dimensions, complexity, and sampling mode), include the following line in the setup method:

block.SetPreCompOutPortInfoToDynamic;

Configure the output ports exactly as you configure input ports. See Creating Input Ports for Level-2 MATLAB S-Functions for a list of properties you can specify for each output port, substituting OutputPort for InputPort in each call to the run-time object.

Scalar Expansion of Inputs

Scalar expansion of inputs refers conceptually to the process of expanding scalar input signals to the same dimensions as wide input signals connected to other S-function input ports. This is done by setting each element of the expanded signal to the value of the scalar input.

A C MEX S-function's mdlInitializeSizes method enables scalar expansion of inputs by setting the SS_OPTION_ALLOW_INPUT_SCALAR_EXPANSION option, using ssSetOptions.

Masked Multiport S-Functions

If you are developing masked multiport S-function blocks whose number of ports varies based on some parameter, and want to place them in a Simulink library, you must specify that the mask modifies the appearance of the block. To do this, execute the command

  set_param(blockname,'MaskSelfModifiable','on')

at the MATLAB command prompt before saving the library, where blockname is the full path to the block. Failure to specify that the mask modifies the appearance of the block means that an instance of the block in a model reverts to the number of ports in the library whenever you load the model or update the library link.

See Also

| | |

Related Topics