%% For Loop
% This example shows how to implement a |for| loop construct  
% by using Simulink blocks, Stateflow Charts, and MATLAB Function blocks.

% Copyright 2019 The MathWorks, Inc.

%% C Construct
% <include>c_construct_for_loop.txt</include> 

%% 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.  
%
% 1. Open example model <matlab:ex_for_loop_SL |ex_for_loop_SL|>.
open_system('ex_for_loop_SL');

%%
% The model contains a For Iterator Subsystem block that repeats execution
% of the contents of the subsystem during a simulation time step.
open_system('ex_for_loop_SL/For Iterator Subsystem');

%%
% Observe the following settings in the model:
%
% * Open the For Iterator block. In the Block Parameters dialog box, the 
%   *Index-mode* parameter is |Zero-based| and the *Iteration limit* 
%   parameter is 10.
% * Open the Unit Delay block. In the Block Parameters dialog box, the 
%   *Initial Conditions* parameter is 0. This parameter initializes 
%   the state to zero.
%%
% 2. To build the model and generate code, press *Ctrl+B*.
evalc('rtwbuild(''ex_for_loop_SL'')');

%%
% The code implementing the |for| loop is in the |ex_for_loop_SL_step| 
% function in |ex_for_loop_SL.c|:
file = fullfile('ex_for_loop_SL_ert_rtw','ex_for_loop_SL.c');
rtwdemodbtype(file,'/* External inputs (root inport signals with default storage) */', ...
    '/* Model initialize function */',1,0);

%% Modeling Pattern for For Loop: Stateflow Chart
% 1. Open example model <matlab:ex_for_loop_SF |ex_for_loop_SF|>.
open_system('ex_for_loop_SF');
%%
% The chart contains a |For| loop decision pattern that you add by right
% clicking inside the chart > *Add Pattern in Chart* > *Loop* > *For*.
%
% 2. To build the model and generate code, press *Ctrl+B*.
evalc('rtwbuild(''ex_for_loop_SF'')');

%%
% The code implementing the |for| loop is in the |ex_for_loop_SF_step| 
% function in |ex_for_loop_SF.c|:
file = fullfile('ex_for_loop_SF_ert_rtw','ex_for_loop_SF.c');
rtwdemodbtype(file,'/* External inputs (root inport signals with default storage) */', ...
    '/* Model initialize function */',1,0);

%% Modeling Pattern for For Loop: MATLAB Function block
% 1. Open example model <matlab:ex_for_loop_ML |ex_for_loop_ML|>.
open_system('ex_for_loop_ML'); 
%%
% The MATLAB Function Block contains this function:
%
% <include>for_loop.m</include>
%%
% 2. To build the model and generate code, press *Ctrl+B*.
evalc('rtwbuild(''ex_for_loop_ML'')');

%%
% The code implementing the |for| loop is in the |ex_for_loop_ML_step| 
% function in |ex_for_loop_ML.c|:
file = fullfile('ex_for_loop_ML_ert_rtw','ex_for_loop_ML.c');
rtwdemodbtype(file,'/* Exported block signals */', ...
    '/* Model initialize function */',1,0);




