Main Content

このページの内容は最新ではありません。最新版の英語を参照するには、ここをクリックします。

while ループ

この例では、Simulink® ブロック、Stateflow® チャート、および MATLAB® Function ブロックを使用して while ループ構造を実装する方法を示します。

C コンストラクター

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

while ループのモデル化パターン: While Iterator Subsystem ブロック

while ループを作成する方法の 1 つとして、[Simulink]、[Ports and Subsystems] ライブラリの While Iterator Subsystem ブロックを使用する方法があります。

1. モデル例 ex_while_loop_SL を開きます。

モデルには、シミュレーションのタイム ステップでサブシステムの内容の実行を繰り返す While Iterator Subsystem ブロックが含まれています。

モデルで次の設定を確認します。

  • Constant ブロックから While Iterator Subsystem に初期条件が提供されます。Constant ブロックの [定数値]1[出力データ型]boolean になっています。初期条件はブロックへの入力に依存します。

  • While Iterator Subsystem の func サブシステム ブロックの出力 flagfunc( ) のアルゴリズムの結果に応じて 0 または 1 になります。func()func サブシステムの [関数名] です。

  • While Iterator Subsystem の While Iterator ブロックの [最大反復回数]100 になっています。

  • While Iterator ブロックの [While ループ タイプ]while になっています。

2. モデルをビルドしてコードを生成するには、Ctrl+B を押します。

while ループを実装するコードが ex_while_loop_SL.c の関数 ex_while_loop_SL_step に含まれています。

/* Model step function */
void ex_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;

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

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

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

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

while ループのモデル化パターン: Stateflow チャート

1. モデル例 ex_while_loop_SF を開きます。

モデルの ex_while_loop_SF/Chartwhile ループが実行されます。

チャートには While ループの判定パターンが含まれています。これは、チャート内で右クリックし、[パターンをチャートに追加][ループ][While] を選択して追加します。

2. モデルをビルドしてコードを生成するには、Ctrl+B を押します。

while ループを実装するコードが ex_while_loop_SF.c の関数 ex_while_loop_SF_step に含まれています。

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

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

  /* End of Chart: '<Root>/Chart' */
}

while ループのモデル化パターン: MATLAB Function ブロック

1. モデル例 ex_while_loop_ML を開きます。

MATLAB 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. モデルをビルドしてコードを生成するには、Ctrl+B を押します。

while ループを実装するコードが ex_while_loop_ML.c の関数 ex_while_loop_ML_step に含まれています。

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

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

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

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

参考

関連する例

詳細