while loop matlab,While Loop

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

1. Open example model ex_while_loop_SL.

82d342573a1a328d1582d725a3c472e9.png

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

d80f4c6a98cc8ebb9fba5d4ee896d7fe.png

Observe the following settings in the model:

The Constant block provides an initial condition to the While Iterator Subsystem. For the Constant block, the Constant value is 1 and the Output data type is boolean. The initial condition can be dependent on the input to the block.

In the While Iterator Subsystem, the func subsystem block has an output flag of 0 or 1 depending on the result of the algorithm in func( ). func() is the Function name in func subsystem.

In the While Iterator Subsystem, for the While Iterator block, the Maximum number of iterations is 100.

For the While Iterator block, the While loop type is while.

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:

* WhileIterator: '/While Iterator'

*/

s1_iter = 1;

/* 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: '/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.

af69c2cf0e356886059fc67950101608.png

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

45d8d68861830c1f8dde3d80c533a739.png

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: '/func' */

num_iter++;

}

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

}

Modeling Pattern for For Loop: MATLAB Function block

1. Open example model ex_while_loop_ML.

395167d6b009a9fca28346d99a946bcf.png

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: '/func' */

flag = func_flag_0;

num_iter++;

}

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

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值