Main Content

Configure Dimension Variants for S-Function Blocks

To configure symbolic dimensions for S-function blocks, you can use the following C/C++ functions. You can configure S-functions to support forward propagation, backward propagation, or forward and backward propagation of symbolic dimensions during simulation.

Many of these functions return the variable SymbDimsId. A SymbDimsId is a unique integer value. This value corresponds to each symbolic dimension specification that you create or is the result of a mathematical operation that you perform with symbolic dimensions.

C/C++ S-FunctionsPurpose

ssSetSymbolicDimsSupport

Specify whether or not an S-function supports symbolic dimensions.

mdlSetInputPortSymbolicDimensions

Specify the symbolic dimensions of an input port and how those dimensions propagate forward.

mdlSetOutputPortSymbolicDimensions

Specify the symbolic dimensions of an output port and how those dimensions propagate backward.

ssRegisterSymbolicDimsExpr

Create a SymbDimsId from an expression string (aExpr). The expression string must form a valid syntax in C.

ssRegisterSymbolicDims

Create a SymbDimsId from a vector of SymbDimsIds.

ssRegisterSymbolicDimsString

Create a SymbDimsId from an identifier string (aString).

ssRegisterSymbolicDimsIntValue

Create a SymbDimsId from an integer value (aIntValue)

ssRegisterSymbolicDimsPlus

Create a SymbDimsId by adding two symbolic dimensions.

ssRegisterSymbolicDimsMinus

Create a SymbDimsId by subtracting two symbolic dimensions.

ssRegisterSymbolicDimsMultiply

Create a SymbDimsId by multiplying two symbolic dimensions.

ssRegisterSymbolicDimsDivide

Create a SymbDimsId by dividing two symbolic dimensions.

ssGetNumSymbolicDims

Get the number of dimensions for a SymbDimsId.

ssGetSymbolicDim

Get a SymbDimsId from a vector of SymbDimsIds.

ssSetInputPortSymbolicDimsId

Set the precompiled SymbDimsId of an input port. You can call this function from inside the mdlInitializeSizes function.

ssGetCompInputPortSymbolicDimsId

Get the compiled SymbDimsId of an input port.

ssSetCompInputPortSymbolicDimsId

Set the compiled SymbDimsId of an input port.

ssSetOutputPortSymbolicDimsId

Set the precompiled SymbDimsId of an output port. You can call this function from inside the mdlInitializeSizes function.

ssGetCompOutputPortSymbolicDimsId

Get the compiled SymbDimsId of an output port.

ssSetCompOutputPortSymbolicDimsId

Set the compiled SymbDimsId of an output port.

ssSetCompDWorkSymbolicDimsId

Set the compiled SymbDimsId of an index of a block’s data type work (DWork) vector.

S-Function That Supports Forward Propagation of Symbolic Dimensions

This S-function subtracts the symbolic dimension B from a symbolic input dimension. It does not support backward propagation of symbolic dimensions because the compiled symbolic dimensions of the input port are not set. Symbolic dimensions are set for the output port, so forward propagation occurs.

static void mdlInitializeSizes(SimStruct *S)
{
     // Enable symbolic dimensions for the s-function.
     ssSetSymbolicDimsSupport(S, true);
}

#if defined(MATLAB_MEX_FILE)
#define MDL_SET_INPUT_PORT_SYMBOLIC_DIMENSIONS
static void mdlSetInputPortSymbolicDimensions(SimStruct* S, 
   int_T portIndex, SymbDimsId symbDimsId)
{
    assert(0 == portIndex);
    // Set the compiled input symbolic dimension.
    ssSetCompInputPortSymbolicDimsId(S,  portIndex, symbDimsId);
    // Register "B" and get its symbolic dimensions id.
    const SymbDimsId symbolIdForB = ssRegisterSymbolicDimsString(S, "B");
   // Subtract "B" from the input symbolic dimension.
    const SymbDimsId outputDimsId = 
        ssRegisterSymbolicDimsMinus(S, symbDimsId, symbolIdForB);
   //Set the resulting symbolic dimensions id as the output.
    ssSetCompOutputPortSymbolicDimsId(S, portIndex, outputDimsId);
}
#endif

#if defined(MATLAB_MEX_FILE)
#define MDL_SET_OUTPUT_PORT_SYMBOLIC_DIMENSIONS
static void mdlSetOutputPortSymbolicDimensions(SimStruct *S, 
   int_T portIndex, SymbDimsId symbDimsId)
{
    assert(0 == portIndex);
   // The input dimensions are not set, so this S-function only
	// supports forward propagation.
    ssSetCompOutputPortSymbolicDimsId(S, portIndex, symbDimsId);
}
#endif

S-Function That Supports Forward and Backward Propagation of Symbolic Dimensions

This S-function transposes two symbolic dimensions. It supports forward and backward propagation of symbolic dimensions because the compiled symbolic dimension of both the input and output ports are set.

static void mdlInitializeSizes(SimStruct *S)
{
     // Enable symbolic dimensions for the s-function.
     ssSetSymbolicDimsSupport(S, true);
}

#if defined(MATLAB_MEX_FILE)
#define MDL_SET_INPUT_PORT_SYMBOLIC_DIMENSIONS
static void mdlSetInputPortSymbolicDimensions(SimStruct* S, 
   int_T portIndex, SymbDimsId symbDimsId)
{
    assert(0 == portIndex);
    
    ssSetCompInputPortSymbolicDimsId(S,  portIndex, symbDimsId);
    
    assert(2U == ssGetNumSymbolicDims(S, symbDimsId));
    
    if (SL_INHERIT == 
        ssGetCompOutputPortSymbolicDimsId(S, portIndex)) {

        const SymbDimsId idVec[] = {
            ssGetSymbolicDim(S, symbDimsId, 1),
            ssGetSymbolicDim(S, symbDimsId, 0)};
        // Register the transposed dimensions.
       // Set the output symbolic dimension to the resulting id.
  const SymbDimsId outputDimsId = 
            ssRegisterSymbolicDims(S, idVec, 2U);

        ssSetCompOutputPortSymbolicDimsId(S, portIndex, 
           outputDimsId);
    }
}
#endif

#if defined(MATLAB_MEX_FILE)
#define MDL_SET_OUTPUT_PORT_SYMBOLIC_DIMENSIONS
static void mdlSetOutputPortSymbolicDimensions(SimStruct *S, 
    int_T portIndex, SymbDimsId symbDimsId)
{
    assert(0 == portIndex);
    ssSetCompOutputPortSymbolicDimsId(S, portIndex, symbDimsId);

    assert(2U == ssGetNumSymbolicDims(S, symbDimsId));
    
    if (SL_INHERIT == 
        ssGetCompInputPortSymbolicDimsId(S, portIndex)) {

        const SymbDimsId idVec[] = {
            ssGetSymbolicDim(S, symbDimsId, 1),
            ssGetSymbolicDim(S, symbDimsId, 0)};
        const SymbDimsId inputDimsId = 
            ssRegisterSymbolicDims(S, idVec, 2U);
        // Register the transposed dimensions.
       // Set the input symbolic dimension to the resulting id.
        ssSetCompInputPortSymbolicDimsId(S, portIndex, inputDimsId);
    }
}
#endif

Related Topics