Main Content

Switch

This example shows how to implement a switch construct by using Simulink® blocks, Stateflow® Charts, and MATLAB® Function block.

C Construct

switch (u1)
{
 case 2:
   y1 = u2;
   break;
 case 3:
   u3;
   break;
 default:
   y1 = u4;
   break;
}

Modeling Pattern for Switch: Switch Case block

One method to create a switch statement is to use a Switch Case block from the Simulink > Signal Routing library.

1. Open example model ex_switch_SL.

The model contains a Switch Case Action Subsystem. The Switch Case block takes an integer input, therefore, the input signal u1 is type cast to int32.

2. To build the model and generate code, press Ctrl+B.

The code implementing the switch construct is in the ex_switch_SL_step function in ex_switch_SL.c:

/* Exported block signals */
int32_T u1;                            /* '<Root>/u1' */

/* External inputs (root inport signals with default storage) */
ExternalInputs rtU;

/* External outputs (root outports fed by signals with default storage) */
ExternalOutputs rtY;

/* Model step function */
void ex_switch_SL_step(void)
{
  /* SwitchCase: '<Root>/Switch Case' incorporates:
   *  Inport: '<Root>/u1'
   */
  switch (u1) {
   case 2:
    /* Outputs for IfAction SubSystem: '<Root>/Switch Case Action Subsystem' incorporates:
     *  ActionPort: '<S1>/Action Port'
     */
    /* Outport: '<Root>/y1' incorporates:
     *  Inport: '<Root>/u2'
     *  SignalConversion generated from: '<S1>/u2'
     */
    rtY.y1 = rtU.u2;

    /* End of Outputs for SubSystem: '<Root>/Switch Case Action Subsystem' */
    break;

   case 3:
    /* Outputs for IfAction SubSystem: '<Root>/Switch Case Action Subsystem1' incorporates:
     *  ActionPort: '<S2>/Action Port'
     */
    /* Outport: '<Root>/y1' incorporates:
     *  Inport: '<Root>/u3'
     *  SignalConversion generated from: '<S2>/u3'
     */
    rtY.y1 = rtU.u3;

    /* End of Outputs for SubSystem: '<Root>/Switch Case Action Subsystem1' */
    break;

   default:
    /* Outputs for IfAction SubSystem: '<Root>/Switch Case Action Subsystem2' incorporates:
     *  ActionPort: '<S3>/Action Port'
     */
    /* Outport: '<Root>/y1' incorporates:
     *  Inport: '<Root>/u4'
     *  SignalConversion generated from: '<S3>/u4'
     */
    rtY.y1 = rtU.u4;

    /* End of Outputs for SubSystem: '<Root>/Switch Case Action Subsystem2' */
    break;
  }

  /* End of SwitchCase: '<Root>/Switch Case' */
}

Modeling Pattern for Switch: MATLAB Function Block

1. Open example model ex_switch_ML.

The MATLAB Function Block contains this function:

function y1 = fcn(u1, u2, u3, u4)

switch u1
    case 2
        y1 = u2;
    case 3
        y1 = u3;
    otherwise
        y1 = u4;
end


2. To build the model and generate code, press Ctrl+B.

The code implementing the switch construct is in the ex_switch_ML_step function in ex_switch_ML.c:

/* External inputs (root inport signals with default storage) */
ExternalInputs rtU;

/* External outputs (root outports fed by signals with default storage) */
ExternalOutputs rtY;

/* Model step function */
void ex_switch_ML_step(void)
{
  /* MATLAB Function: '<Root>/MATLAB Function' incorporates:
   *  Inport: '<Root>/u1'
   */
  switch (rtU.u1) {
   case 2:
    /* Outport: '<Root>/y1' incorporates:
     *  Inport: '<Root>/u2'
     */
    rtY.y1 = rtU.u2;
    break;

   case 3:
    /* Outport: '<Root>/y1' incorporates:
     *  Inport: '<Root>/u3'
     */
    rtY.y1 = rtU.u3;
    break;

   default:
    /* Outport: '<Root>/y1' incorporates:
     *  Inport: '<Root>/u4'
     */
    rtY.y1 = rtU.u4;
    break;
  }

  /* End of MATLAB Function: '<Root>/MATLAB Function' */
}

Convert If-Elseif-Else to Switch statement

If a MATLAB Function block or a Stateflow chart uses if-elseif-else decision logic, you can convert the block or chart to a switch statement by using a configuration parameter. Select the Configuration Parameters > Code Generation > Code Style > Convert if-elseif-else patterns to switch-case statements parameter. For more information, see Converting If-Elseif-Else Code to Switch-Case Statements. For more information on this conversion by using a Stateflow chart, see Enhance Readability of Code for Flow Charts.

See Also

Related Topics