Products & Services Solutions Academia Support User Community Company

Learn more about Real-Time Workshop Embedded Coder   

Control Flow

If-Else

C Construct

if (u1 > u2)
{
		y1 = u1;
}
else
{
		y1 = u2;
}

Modeling Patterns

Modeling Pattern for If-Else: Switch block

One method to create an if-else statement is to use a Switch block from the Simulink > Signal Routing library.

Model If_Else_SL

Procedure.  

  1. Drag the Switch block from the Simulink>Signal Routing library into your model.

  2. Connect the data inputs and outputs to the block.

  3. Drag a Relational Operator block from the Logic & Bit Operations library into your model.

  4. Connect the signals that are used in the if-expression to the Relational Operator block. The order of connection determines the placement of each signal in the if-expression.

  5. Configure the Relational Operator block to be a greater than operator.

  6. Connect the controlling input to the middle input port of the Switch block.

  7. Double-click the Switch block and set Criteria for passing first input to u2~=0. This condition ensures that Simulink selects u1 if u2 is TRUE; otherwise u2 passes.

  8. Enter Ctrl+B to build the model and generate code.

Results.  Real-Time Workshop software generates the following If_Else_SL_step function in the file If_Else_SL.c:

   /* External inputs (root inport signals with auto storage) */
   ExternalInputs U;
      
   /* External outputs (root outports fed by signals with auto storage) */
   ExternalOutputs Y;
      
   /* Model step function */
   void If_Else_SL_step(void)
   {
     /* Switch: '<Root>/Switch' incorporates:
      *  Inport: '<Root>/u1'
      *  Inport: '<Root>/u2'
      *  Outport: '<Root>/y1'
      *  RelationalOperator: '<Root>/Relational Operator'
      */
    if (U.u1 > U.u2) {
       Y.y1 = U.u1;
     } else {
       Y.y1 = U.u2;
     }
   }

Modeling Pattern for If-Else: Stateflow Chart

If-Else Stateflow Chart

Procedure.  

  1. Follow the steps forSetting Up an Example Model With a Stateflow Chart. This example model contains two Inport blocks and one Outport block.

  2. Name your model If_Else_SF.

  3. When configuring your Stateflow chart, select Patterns > Add Decision > If-Else. The Stateflow Pattern dialog opens. Fill in the fields as follows:

    DescriptionIf-Else (optional)
    If conditionu1 > u2
    If actiony1 = u1
    Else actiony1 = u2

  4. Press Ctrl+B to build the model and generate code.

Results.  Real-Time Workshop software generates the following If_Else_SF_step function in the file If_Else_SF.c:

   /* External inputs (root inport signals with auto storage) */
   ExternalInputs U;
      
   /* External outputs (root outports fed by signals with auto storage) */
   ExternalOutputs Y;
      
   /* Model step function */
   void If_Else_SF_step(void)
   {
     /* Stateflow: '<Root>/Chart' incorporates:
      *  Inport: '<Root>/u1'
      *  Inport: '<Root>/u2'
      *  Outport: '<Root>/y1'
      */
     /* Gateway: Chart */
     /* During: Chart */
     /* Transition: '<S1>:14' */
     /*  If-Else  */
     if (U.u1 > U.u2) {
       /* Transition: '<S1>:13' */
       /* Transition: '<S1>:12' */
       Y.y1 = U.u1;
      
       /* Transition: '<S1>:11' */
     } else {
       /* Transition: '<S1>:10' */
       Y.y1 = U.u2;
     }
      
     /* Transition: '<S1>:9' */
   }

Modeling Pattern for If-Else: Embedded MATLAB Block

Procedure.  

  1. Follow the steps for Setting Up an Example Model With an Embedded MATLAB Block. This example model contains two Inport blocks and one Outport block.

  2. Name your model If_Else_EML.

  3. In the Embedded MATLAB editor enter the function, as follows:

    function y1 = fcn(u1, u2)
    if u1 > u2;
      y1 = u1;
    else y1 = u2;
    end
  4. Press Ctrl+B to build the model and generate code.

Results.  Real-Time Workshop software generates the following If_Else_EML_step function in the file If_Else_EML.c:

   /* External inputs (root inport signals with auto storage) */
   ExternalInputs U;
      
   /* External outputs (root outports fed by signals with auto storage) */
   ExternalOutputs Y;
      
   /* Model step function */
   void If_Else_EML_step(void)
   {
     /* Embedded MATLAB: '<Root>/Embedded MATLAB Function' incorporates:
      *  Inport: '<Root>/u1'
      *  Inport: '<Root>/u2'
      *  Outport: '<Root>/y1'
      */
     /* Embedded MATLAB Function 'Embedded MATLAB Function': '<S1>:1' */
     if (U.u1 > U.u2) {
       /* '<S1>:1:4' */
       /* '<S1>:1:5' */
       Y.y1 = U.u1;
     } else {
       /* '<S1>:1:6' */
       Y.y1 = U.u2;
     }
   }

Switch

C Construct

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

Modeling Patterns

Modeling Pattern for Switch: Switch Case block

One method for creating a switch statement is to use a Switch Case block from the Simulink > Ports and Subsystems library.

Model Switch_SL

Procedure.  

  1. Drag a Switch Case block from the Simulink > Ports and Subsystems library into your model.

  2. Double-click the block. In the Block Parameters dialog box, fill in the Case Conditions parameter. In this example, the two cases are: {2,3}.

  3. Select the Show default case parameter. The default case is optional in a switch statement.

  4. Connect the condition input u1 to the input port of the Switch block.

  5. Drag Switch Case Action Subsystem blocks from the Simulink>Ports and Subsystems library to correspond with the number of cases.

  6. Configure the Switch Case Action Subsystem subsystems.

  7. Drag a Merge block from the Simulink > Signal Routing library to merge the outputs.

  8. The Switch Case block takes an integer input, therefore, the input signal u1 is type cast to an int32.

  9. Enter Ctrl+B to build the model and generate code.

Results.  Real-Time Workshop software generates the following Switch_SL_step function in the file Switch_SL.c:

     /* Exported block signals */
      int32_T u1;                            /* '<Root>/u1' */
      
      /* External inputs (root inport signals with auto storage) */
      ExternalInputs U;
      
      /* External outputs (root outports fed by signals with auto storage) */
      ExternalOutputs Y;
      
      /* Model step function */
      void Switch_SL_step(void)
      {
        /* SwitchCase: '<Root>/Switch Case' incorporates:
         *  ActionPort: '<S1>/Action Port'
         *  ActionPort: '<S2>/Action Port'
         *  ActionPort: '<S3>/Action Port'
         *  Inport: '<Root>/u1'
         *  SubSystem: '<Root>/Switch Case Action Subsystem'
         *  SubSystem: '<Root>/Switch Case Action Subsystem1'
         *  SubSystem: '<Root>/Switch Case Action Subsystem2'
         */
        switch (u1) {
         case 2:
          /* Inport: '<S1>/u2' incorporates:
           *  Inport: '<Root>/u2'
           *  Outport: '<Root>/y1'
           */
          Y.y1 = U.u2;
          break;
      
         case 3:
          /* Inport: '<S2>/u3' incorporates:
           *  Inport: '<Root>/u3'
           *  Outport: '<Root>/y1'
           */
          Y.y1 = U.u3;
          break;
      
         default:
          /* Inport: '<S3>/u4' incorporates:
           *  Inport: '<Root>/u4'
           *  Outport: '<Root>/y1'
           */
          Y.y1 = U.u4;
          break;
        }
      }

Modeling Pattern for Switch: Embedded MATLAB block

Procedure.  

  1. Follow the steps for Setting Up an Example Model With an Embedded MATLAB Block. This example model contains four Inport blocks and one Outport block.

  2. Name your model If_Else_EML.

  3. In the Embedded MATLAB editor enter the function, as follows:

    function y1 = fcn(u1, u2, u3, u4)
    
    switch u1
        case 2
            y1 = u2;
        case 3
            y1 = u3;
        otherwise
            y1 = u4;
    end
  4. Press Ctrl+B to build the model and generate code.

Results.  Real-Time Workshop software generates the following Switch_EML_step function in the file Switch_EML.c:

   /* External inputs (root inport signals with auto storage) */
      ExternalInputs U;
      
      /* External outputs (root outports fed by signals with auto storage) */
      ExternalOutputs Y;
      
      /* Model step function */
      void Switch_EML_step(void)
      {
        /* Embedded MATLAB: '<Root>/Embedded MATLAB Function' incorporates:
         *  Inport: '<Root>/u1'
         *  Inport: '<Root>/u2'
         *  Inport: '<Root>/u3'
         *  Inport: '<Root>/u4'
         *  Outport: '<Root>/y1'
         */
        /* Embedded MATLAB Function 'Embedded MATLAB Function': '<S1>:1' */
        /* '<S1>:1:4' */
        switch (U.u1) {
         case 2:
          /* '<S1>:1:6' */
          Y.y1 = U.u2;
          break;
      
         case 3:
          /* '<S1>:1:8' */
          Y.y1 = U.u3;
          break;
      
         default:
          /* '<S1>:1:10' */
          Y.y1 = U.u4;
          break;
        }
      }

Converting If-Elseif-Else to Switch statement

If an Embedded MATLAB Function block or a Stateflow chart contains uses if-elseif-else decision logic, you can convert it to a switch statement by using a configuration parameter. In the Configuration Parameters dialog box, on the Real-Time Workshop > Code Style pane, select the Convert if-elseif-else patterns to switch-case statements parameter. For more information, see Converting If-Elseif-Else Code to Switch-Case Statements in the Simulink documentation. For more information on this conversion using a Stateflow chart, see Converting If-Elseif-Else Code to Switch-Case Statements and Example of Converting Code for If-Elseif-Else Decision Logic to Switch-Case Statements in the Stateflow documentation.

For loop

C Construct

y1 = 0;
for(inx = 0; inx <10; inx++)
{
		y1 = u1[inx] + y1;
}

Modeling Patterns:

Modeling Pattern for For Loop: For-Iterator Subsystem block

One method for creating a for loop is to use a For Iterator Subsystem block from the Simulink > Ports and Subsystems library.

Model For_Loop_SL

For Iterator Subsystem

Procedure.  

  1. Drag a For Iterator Subsystem block from the Simulink > Ports and Subsystems library into your model.

  2. Connect the data inputs and outputs to the For Iterator Subsystem block.

  3. Open the Inport block.

  4. In the Block Parameters dialog box, select the Signal Attributes pane and set the Port dimensions parameter to 10.

  5. Double-click the For Iterator Subsystem block to open the subsystem.

  6. Drag an Index Vector block from the Signal-Routing library into the subsystem.

  7. Open the For Iterator block. In the Block Parameters dialog box set the Index-mode parameter to Zero-based and the Iteration limit parameter to 10.

  8. Connect the controlling input to the topmost input port of the Index Vector block, and the other input to the second port.

  9. Drag an Add block from the Math Operations library into the subsystem.

  10. Drag a Unit Delay block from Commonly Used Blocks library into the subsystem.

  11. Double-click the Unit Delay block and set the Initial Conditions parameter to 0. This parameter initializes the state to zero.

  12. Connect the blocks as shown in the model diagram.

  13. Save the subsystem and the model.

  14. Enter Ctrl+B to build the model and generate code.

Results.  Real-Time Workshop software generates the following For_Loop_SL_step function in the file For_Loop_SL.c:

        /* External inputs (root inport signals with auto storage) */
      ExternalInputs U;
      
      /* External outputs (root outports fed by signals with auto storage) */
      ExternalOutputs Y;
      
      /* Model step function */
      void For_Loop_SL_step(void)
      {
        int32_T s1_iter;
        int32_T rtb_y1;
      
        /* Outputs for iterator SubSystem: '<Root>/For Iterator Subsystem' incorporates:
         *  ForIterator: '<S1>/For Iterator'
         */
        for (s1_iter = 0; s1_iter < 10; s1_iter++) {
          /* Sum: '<S1>/Add' incorporates:
           *  Inport: '<Root>/u1'
           *  MultiPortSwitch: '<S1>/Index Vector'
           *  UnitDelay: '<S1>/Unit Delay'
           */
          rtb_y1 = U.u1[s1_iter] + DWork.UnitDelay_DSTATE;
      
          /* Update for UnitDelay: '<S1>/Unit Delay' */
          DWork.UnitDelay_DSTATE = rtb_y1;
        }
      
        /* end of Outputs for SubSystem: '<Root>/For Iterator Subsystem' */
      
        /* Outport: '<Root>/y1' */
        Y.y1 = rtb_y1;
      }

Modeling Pattern for For Loop: Stateflow Chart

Procedure.  

  1. Follow the steps forSetting Up an Example Model With a Stateflow Chart. This example model contains one Inport block and one Outport block.

  2. Name the model For_Loop_SF.

  3. Enter Ctrl+R to open the Model Explorer.

  4. In the Model Explorer, select the output variable, u1, and in the right pane, select the Value Attributes tab and set the Initial Value to 0.

  5. In the Stateflow Editor, select Patterns > Add Loop > For. The Stateflow Pattern dialog opens.

  6. Fill in the fields in the Stateflow Pattern dialog box as follows:

    DescriptionFor Loop (optional)
    Initializer expressioninx = 0
    Loop test expressioninx < 10
    Counting expressioninx++
    For loop bodyy1 = u1[inx] + y1

    The Stateflow diagram is shown.

  7. Press Ctrl+B to build the model and generate code.

Results.  Real-Time Workshop software generates the following For_Loop_SF_step function in the file For_Loop_SF.c:

      /* Block signals (auto storage) */
      BlockIO B;
      
      /* External inputs (root inport signals with auto storage) */
      ExternalInputs U;
      
      /* External outputs (root outports fed by signals with auto storage) */
      ExternalOutputs Y;
      
      /* Model step function */
      void For_Loop_SF_step(void)
      {
        int32_T sf_inx;
      
        /* Stateflow: '<Root>/Chart' incorporates:
         *  Inport: '<Root>/u1'
         */
        /* Gateway: Chart */
        /* During: Chart */
        /* Transition: '<S1>:24' */
        /*  For Loop  */
        /* Transition: '<S1>:25' */
        for (sf_inx = 0; sf_inx < 10; sf_inx++) {
          /* Transition: '<S1>:22' */
          /* Transition: '<S1>:23' */
          B.y1 = U.u1[sf_inx] + B.y1;
      
          /* Transition: '<S1>:21' */
        }

        /* Transition: '<S1>:20' */
      
        /* Outport: '<Root>/y1' */
        Y.y1 = B.y1;
      }

Modeling Pattern for For Loop: Embedded MATLAB block

Procedure.  

  1. Follow the directions for Setting Up an Example Model With an Embedded MATLAB Block. This example model contains one Inport block and one Outport block.

  2. Name your model For_Loop_EML.

  3. In the Embedded MATLAB editor enter the function, as follows:

    function y1 = fcn(u1)
     
    y1 = 0;
     
    for inx=1:10
        y1 = u1(inx) + y1 ;
    end
  4. Press Ctrl+B to build the model and generate code.

Results.  Real-Time Workshop software generates the following For_Loop_EML_step function in the file For_Loop_EML.c:

      /* Exported block signals */
      real_T u1[10];                         /* '<Root>/u1' */
      real_T y1;                             /* '<Root>/Embedded MATLAB Function' */
      
      /* Model step function */
      void For_Loop_EML_step(void)
      {
        int32_T eml_inx;
      
        /* Embedded MATLAB: '<Root>/Embedded MATLAB Function' incorporates:
         *  Inport: '<Root>/u1'
         */
        /* Embedded MATLAB Function 'Embedded MATLAB Function': '<S1>:1' */
        /* '<S1>:1:3' */
        y1 = 0.0;
        for (eml_inx = 0; eml_inx < 10; eml_inx++) {
          /* '<S1>:1:5' */
          /* '<S1>:1:6' */
          y1 = u1[eml_inx] + y1;
        }
      }

While loop

C Construct

while(flag && (num_iter <= 100)
{
		flag = func ();
		num_iter ++;
}

Modeling Patterns

Modeling Pattern for While Loop: While Iterator Subsystem block

One method for creating a while loop is to use a While Iterator Subsystem block from the Simulink > Ports and Subsystems library.

Model While_Loop_SL

While_Loop_SL/While Iterator Subsystem

Procedure.  

  1. Drag a While Iterator Subsystem block from the Simulink > Ports and Subsystems library into the model.

  2. Drag a Constant block from the Simulink > Commonly Used Blocks library into the model. In this case, set the Initial Condition to 1 and the Data Type to Boolean. You do not always have to set the initial condition to FALSE. The initial condition can be dependent on the input to the block.

  3. Connect the Constant block to the While Iterator Subystem block.

  4. Double-click the While Iterator Subsystem block to open the subsystem.

  5. Place a Subsystem block next to the While Iterator block.

  6. Right-click the subsystem and select Subsystem Parameters. The Block Parameters dialog box opens.

  7. Select the Treat as atomic unit parameter to configure the subsystem to generate a function.

  8. From the Real-Time Workshop system code list, select the option, Function.

  9. From the Real-Time Workshop function name options list, select the option, User specified. The Real-Time Workshop function name parameter is displayed.

  10. Specify the name as func.

  11. Click Apply.

  12. Double-click the func() subsystem block. In this example, function func() has an output flag set to 0 or 1 depending on the result of the algorithm in func( ). Create the func() algorithm as shown in the following diagram:

    func()

  13. Double-click the While Iterator block to set the Maximum number of iterations to 100.

  14. Connect blocks as shown in the model and subsystem diagrams.

Results.  Real-Time Workshop software generates the following While_Loop_SL_step function in the file While_Loop_SL.c:

  /* Exported block signals */
  boolean_T IC;                          /* '<Root>/Initial Condition SET to TRUE' */
  boolean_T flag;                        /* '<S2>/Relational Operator' */
     
  /* Block states (auto storage) */
  D_Work DWork;
      
  /* Start for atomic system: '<S1>/func( ) Is a function that updates the flag' */
  void func_Start(void)
  {
    /* Start for RandomNumber: '<S2>/Random Number' */
    DWork.RandSeed = 1144108930U;
    DWork.NextOutput = rt_NormalRand(&DWork.RandSeed) * 1.7320508075688772E+000;
  }
      
  /* Output and update for atomic system: 
   *  '<S1>/func( ) Is a function that updates the flag' */
  void func(void)
  {
    /* RelationalOperator: '<S2>/Relational Operator' incorporates:
     *  Constant: '<S2>/Constant1'
     *  RandomNumber: '<S2>/Random Number'
     */
    flag = (DWork.NextOutput > 1.0);
      
    /* Update for RandomNumber: '<S2>/Random Number' */
    DWork.NextOutput = rt_NormalRand(&DWork.RandSeed) * 1.7320508075688772E+000;
  }
      
  /* Model step function */
  void While_Loop_SL_step(void)
  {
    int32_T s1_iter;
    boolean_T loopCond;
      
    /* Outputs for iterator SubSystem: 
     *     '<Root>/While Iterator Subsystem' incorporates:
     *  WhileIterator: '<S1>/While Iterator'
     */
    s1_iter = 1;
    loopCond = IC;
    while (loopCond && (s1_iter <= 100)) {
      /* Outputs for atomic SubSystem: 
       * '<S1>/func( ) Is a function that updates the flag' */
      func();
      
      /* end of Outputs for SubSystem: 
       * '<S1>/func( ) Is a function that updates the flag' */
      loopCond = flag;
      s1_iter++;
    }
      
    /* end of Outputs for SubSystem: '<Root>/While Iterator Subsystem' */
  }

Modeling Pattern for While Loop: Stateflow Chart

Model While_Loop_SF

While_Loop_SF/Chart Executes the desired while-loop

Procedure.  

  1. Add a Stateflow Chart to your model from the Stateflow > Stateflow Chart library.

  2. Double-click the chart and select Tools > Explore or enter Ctrl+R to open the Model Explorer.

  3. Add the inputs and outputs to the chart and specify their data type.

  4. Connect the data input and output to the Stateflow chart as shown in the model diagram.

  5. In the Model Explorer, select the output variable, then, in the right pane, select the Value Attributes tab and set the Initial Value to 0.

  6. Select Patterns > Add Loop > While. The Stateflow Pattern dialog opens.

  7. Fill in the fields for the Stateflow Pattern dialog box as follows:

    DescriptionWhile Loop (optional)
    While condition(flag) && (num_iter<=100)
    Do actionfunc; num_iter++;

  8. Place a Subsystem block in your model.

  9. Right-click the subsystem and select Subsystem Parameters. The Block Parameters dialog box opens.

  10. Select the Treat as atomic unit parameter to configure the subsystem to generate a function.

  11. From the Real-Time Workshop system code list, select the option, Function.

  12. From the Real-Time Workshop function name options list, select the option, User specified. The Real-Time Workshop function name parameter is displayed.

  13. Specify the name as func.

  14. Click Apply to apply all changes.

  15. Double-click the func() subsystem block. In this example, function func() has an output flag set to 0 or 1 depending on the result of the algorithm in func( ). The Trigger block parameter Trigger type is function-call. Create the func() algorithm, as shown in the following diagram:

    While_Loop_SF/func() A function that updates the flag

  16. Save and close the subsystem.

  17. Connect blocks to the Stateflow chart as shown in the model diagram for While_Loop_SF.

  18. Save your model.

Results.  Real-Time Workshop software generates the following While_Loop_SF_step function in the file While_Loop_SF.c:

      /* Exported block signals */
      int32_T num_iter;  /* '<Root>/Chart Executes the desired while-loop' */
      boolean_T flag;    /* '<S2>/Relational Operator' */
      
      /* Block states (auto storage) */
      D_Work DWork;
      
      /* Model step function */
      void While_Loop_SF_step(void)
      {
        /* Stateflow: '<Root>/Chart Executes the desired 
         * while-loop' incorporates:
         *  SubSystem: '<Root>/func() A function that  
         *              updates the flag'
         */
        /* Gateway: Chart
           Executes the desired while-loop */
        /* During: Chart
           Executes the desired while-loop */
        /* Transition: '<S1>:2' */
        num_iter = 1;
        while (flag && (num_iter <= 100)) {
          /* Transition: '<S1>:3' */
          /* Transition: '<S1>:4' */
          /* Event: '<S1>:12' */
          func();
          num_iter = num_iter + 1;
      
          /* Transition: '<S1>:5' */
        }
      
        /* Transition: '<S1>:1' */
      }   

Modeling Pattern for While Loop: Embedded MATLAB block

Model While_Loop_EML

Procedure.  

  1. In the Simulink Library Browser, click Simulink > User Defined Functions, and drag an Embedded MATLAB Function block into your model.

  2. Double-click the Embedded MATLAB Function block. The Embedded MATLAB editor opens.

  3. In the Embedded MATLAB editor enter the function, as follows:

    function fcn(func_flag)
    
    flag = true; 
    num_iter = 1;
    
    while(flag && (num_iter<=100))
        func;
        flag = func_flag;
        num_iter = num_iter + 1;
    end
  4. Click File > Save and close the Embedded MATLAB editor.

  5. Place a Subsystem block in your model, right-click the subsystem and select Subsystem Parameters. The Block Parameters dialog box opens.

  6. Select the Treat as atomic unit parameter to configure the subsystem to generate a function.

  7. From the Real-Time Workshop system code list, select the option, Function.

  8. From the Real-Time Workshop function name options list, select the option, User specified. The Real-Time Workshop function name parameter is displayed.

  9. Specify the name as func.

  10. Click Apply.

  11. Double-click the func() subsystem block. In this example, function func() has an output flag set to 0 or 1 depending on the result of the algorithm in func( ). The Trigger block parameter Trigger type is function-call. Create the func() algorithm, as shown in the following diagram:

  12. Save and close the subsystem.

  13. Connect the Embedded MATLAB Function block to the func() subsystem.

  14. Save your model.

Results.  Real-Time Workshop software generates the following While_Loop_EML_step function in the file While_Loop_EML.c. In some cases an equivalent for loop might be generated instead of a while loop.

/* Exported block signals */
boolean_T func_flag;               /* '<S2>/Relational Operator' */
      
/* Block states (auto storage) */
D_Work DWork;
      
/* Model step function */
void While_Loop_EML_step(void)
{
  boolean_T eml_func_flag;
  boolean_T eml_flag;
  int32_T eml_num_iter;
      
  /* Embedded MATLAB: '<Root>/Embedded MATLAB Function Executes
   * the desired While-Loop' incorporates:
   *  SubSystem: '<Root>/func() updates the "flag"'
   */
  eml_func_flag = func_flag;
      
  /* Embedded MATLAB Function 'Embedded MATLAB Function 
   * Executes the desired While-Loop': '<S1>:1' */
  /* '<S1>:1:3' */
   eml_flag = true;
      
  /* '<S1>:1:4' */
  for (eml_num_iter = 1; eml_flag && (eml_num_iter <= 100); 
       eml_num_iter++) {
    /* '<S1>:1:6' */
    /* '<S1>:1:7' */
    func();
      
    /* '<S1>:1:8' */
    eml_flag = eml_func_flag;
      
    /* '<S1>:1:9' */
  }
}

Do While loop

C Construct

num_iter = 1;
do {
   flag = func();
   num_iter++;
   }
while (flag && num_iter <= 100)

Modeling Patterns

Modeling Pattern for Do While Loop: While Iterator Subsystem block

One method for creating a while loop is to use a While Iterator Subsystem block from the Simulink > Ports and Subsystems library.

Do_While_Loop_SL

Do_While_Loop_SL/While Iterator Subsystem

Procedure.  

  1. Drag a While Iterator Subsystem block from the Simulink > Ports and Subsystems library into the model.

  2. Double-click the While Iterator Subsystem block to open the subsystem.

  3. Place a Subsystem block next to the While Iterator block.

  4. Right-click the subsystem and select Subsystem Parameters. The Block Parameters dialog box opens.

  5. Select the Treat as atomic unit parameter to configure the subsystem to generate a function.

  6. From the Real-Time Workshop system code list, select the option, Function.

  7. From the Real-Time Workshop function name options list, select the option, User specified. The Real-Time Workshop function name parameter is displayed.

  8. Specify the name as func.

  9. Click Apply.

  10. Double-click the func() subsystem block. In this example, function func() has an output flag set to 0 or 1 depending on the result of the algorithm in func( ). Create the func() algorithm as shown in the following diagram:

    Do_While_Loop_SL/While Iterator Subsystem/func()

  11. Double-click the While Iterator block. This opens the Block Parameters dialog.

  12. Set the Maximum number of iterations to 100.

  13. Specify the While loop type as do-while.

  14. Connect blocks as shown in the model and subsystem diagrams.

  15. Enter Ctrl+B to generate code.

Results.  

void func(void)
{
   flag = (DWork.NextOutput > (real_T)P.Constant1_Value);
   DWork.NextOutput = 
      rt_NormalRand(&DWork.RandSeed) * P.RandomNumber_StdDev +
      P.RandomNumber_Mean;
}

void Do_While_Loop_SL_step(void)
{
   int32_T s1_iter;

   s1_iter = 1;
   do {
      func();
      s1_iter++;
      } while (flag && (s1_iter <= 100));

}

Modeling Pattern for Do While Loop: Stateflow Chart

Do_While_Loop_SF

Do_While_Loop_SF/Chart Executes the desired do-while-loop

  1. Add a Stateflow Chart to your model from the Stateflow > Stateflow Chart library.

  2. Double-click the chart and select Tools > Explore or enter Ctrl+B to open the Model Explorer.

  3. Add the inputs and outputs to the chart and specify their data type.

  4. Connect the data input and output to the Stateflow chart.

  5. In the Model Explorer, select the output variable, then, in the right pane, select the Value Attributes tab and set the Initial Value to 0.

  6. Select Patterns > Add Loop > While. The Stateflow Pattern dialog opens.

  7. Fill in the fields for the Stateflow Pattern dialog box as follows:

    DescriptionWhile Loop (optional)
    While condition(flag) && (num_iter<=100)
    Do actionfunc; num_iter++;

  8. Place a Subsystem block in your model.

  9. Right-click the subsystem and select Subsystem Parameters. The Block Parameters dialog box opens.

  10. Select the Treat as atomic unit parameter to configure the subsystem to generate a function.

  11. From the Real-Time Workshop system code list, select the option, Function.

  12. From the Real-Time Workshop function name options list, select the option, User specified. The Real-Time Workshop function name parameter is displayed.

  13. Specify the name as func.

  14. Click Apply to apply all changes.

  15. Double-click the func() subsystem block. In this example, function func() has an output flag set to 0 or 1 depending on the result of the algorithm in func( ). The Trigger block parameter Trigger type is function-call. Create the func() algorithm, as shown in the following diagram:

    Do_While_Loop_SF/func() Updates the flag

  16. Save and close the subsystem.

  17. Connect blocks to the Stateflow chart as shown in the model diagram for Do_While_Loop_SF.

  18. Save your model.

Results.  

void Do_While_Loop_SF_step(void)
{
   int32_T sf_num_iter;
   do {
      sf_num_iter = 1;
      func();
      sf_num_iter++;
    } while (flag && (sf_num_iter <= 100));

}
  


Related Products & Applications

Learn more about Simulink through this collection of videos, articles, technical literature and the Getting Started with Simulink Guide.

 © 1984-2010- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS