Main Content


Specify the number of inputs, outputs, states, parameters, and other characteristics of the MATLAB S-function









Instance of Simulink.MSFcnRunTimeBlock class representing the Level-2 MATLAB S-Function block.


This is the first S-function callback methods that the Simulink® engine calls.

The Level-2 MATLAB S-function setup method performs nearly the same tasks as the C MEX S-function mdlInitializeSizes method, with two significant differences. The setup method does not initialize discrete state information, but it does specify the block sample times, eliminating the need for an mdlInitializeSampleTimes method. Use the following properties and methods of the run-time object s to configure the S-function:

  • Specify the number of parameters that this S-function supports, using s.NumDialogPrms.

    Use s.DialogPrmsTunable to set the tunability of each dialog parameter. When a parameter has been specified as not tunable, the Simulink engine issues an error during simulation (or when in external mode when using the Simulink Coder™ product) if an attempt is made to change the parameter.

  • Specify the number of continuous states that this function has, using s.NumContStates. Specify discrete state information in the PostPropagationSetup method using a DWork vector.

  • Configure the block's input ports, including:

    • Specify the number of input ports that this S-function has, using s.NumInputPorts.

    • Specify the dimensions of the ith input port, using s.InputPort(i).Dimensions.

    • If using port-based sample times, specify the sample time of the ith input port, using s.InputPort(i).SampleTime.

    • For each input port, specify whether it has direct feedthrough, using s.InputPort(i).DirectFeedthrough.

      A port has direct feedthrough if the input is used in the Outputs method to calculate the output or the next sample time, for an S-function with a variable sample time. The direct feedthrough flag for each input port can be set to either 1=yes or 0=no. It should be set to 1 if the input, u, is used in the Outputs method. Setting the direct feedthrough flag to 0 tells the engine that u is not used in this S-function method. Violating this leads to unpredictable results.

    See Simulink.BlockData and its parent and children classes for a list of all the properties and methods associated with a Level-2 MATLAB S-function input port.

  • Configure the block's output ports, including:

    • Specify the number of output ports that the block has, using s.NumOutputPorts.

    • Specify the dimensions of the ith output port, using s.OutputPort(i).Dimensions.

    • If using port-based sample times, specify the sample time of the ith output port, using s.OutputPort(i).SampleTime.

  • Set the block-based sample times (i.e., sample rates), using s.SampleTimes.

    See Specify S-Function Sample Times for a complete discussion of sample time issues.

    For multirate S-functions, the suggested approach to setting sample times is via the port-based sample times method. When you create a multirate S-function, you must take care to verify that, when slower tasks are preempted, your S-function correctly manages data so as to avoid race conditions. When port-based sample times are specified, the block cannot inherit a sample time of Inf at any port.

See Using the setup Method for additional information and examples using the setup method.

Dynamically Sized Block Features

You can set the parameters NumContStates, NumDworkDiscStates, NumInputPorts, and NumOutputPorts to a fixed non-negative integer or tell the Simulink engine to size them dynamically:

  • DYNAMICALLY_SIZED -- Sets lengths of states, work vectors, and so on to values inherited from the driving block. It sets widths to the actual input widths, according to the scalar expansion rules unless you use mdlSetWorkWidths to set the widths.

  • 0 or positive number -- Sets lengths (or widths) to the specified values. The default is 0.

Version History

Introduced in R2012b