While Loop - MATLAB & Simulink (original) (raw)

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

C Construct

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

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 & Subsystems library.

1. Open example model ex_while_loop_SL.

The model contains a While Iterator Subsystem block that repeats execution of the contents of the subsystem during a simulation time step.

Observe the following settings in the model:

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

The code implementing the while loop is in the ex_while_loop_SL_step function in ex_while_loop_SL.c:

/* Model step function */ void ex_while_loop_SL_step(void) { int32_T s1_iter; boolean_T loopCond;

/* Outputs for Iterator SubSystem: '/While Iterator Subsystem' incorporates:

/* SystemReset for Atomic SubSystem: '/func' */ func_Reset();

/* End of SystemReset for SubSystem: '/func' */ loopCond = true; while (loopCond && (s1_iter <= 100)) { /* Outputs for Atomic SubSystem: '/func' */ func();

/* End of Outputs for SubSystem: '<S1>/func' */
loopCond = flag;
s1_iter++;

}

/* End of Outputs for SubSystem: '/While Iterator Subsystem' */ }

Modeling Pattern for While Loop: Stateflow Chart

1. Open example model ex_while_loop_SF.

In the model, the ex_while_loop_SF/Chart executes the while loop.

The chart contains a While loop decision pattern that you add by right clicking inside the chart > Add Pattern in Chart > Loop > While.

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

The code implementing the while loop is in the ex_while_loop_SF_step function in ex_while_loop_SF.c:

/* Model step function / void ex_while_loop_SF_step(void) { / Chart: '/Chart' */ num_iter = 1; while (flag && (num_iter <= 100)) { /* Outputs for Function Call SubSystem: '/func' */ func();

/* End of Outputs for SubSystem: '<Root>/func' */
num_iter++;

}

/* End of Chart: '/Chart' */ }

Modeling Pattern for While Loop: MATLAB Function block

1. Open example model ex_while_loop_ML.

The MATLAB Function Block contains this function:

function fcn(func_flag)

flag = true; num_iter = 1;

while(flag && (num_iter<=100)) func; flag = func_flag; num_iter = num_iter + 1; end

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

The code implementing the while loop is in the ex_while_loop_ML_step function in ex_while_loop_ML.c:

/* Model step function */ void ex_while_loop_ML_step(void) { int32_T num_iter; boolean_T flag; boolean_T func_flag_0;

/* MATLAB Function: '/MATLAB Function' */ func_flag_0 = func_flag; flag = true; num_iter = 1; while (flag && (num_iter <= 100)) { /* Outputs for Function Call SubSystem: '/func' */ func();

/* End of Outputs for SubSystem: '<Root>/func' */
flag = func_flag_0;
num_iter++;

}

/* End of MATLAB Function: '/MATLAB Function' */ }

See Also

While Iterator Subsystem

Topics