%% Stateflow Chart Implementing Moore Semantics
% This model contains a Stateflow chart that implements Moore machine
% semantics. The chart contains a MATLAB function that defines the function
% |fcn|, described previously. The input |u| has values of 1 and -1 that
% depend on the state of the chart.

% Copyright 2018-2019 The MathWorks, Inc.

%% 

mdl = 'sf_init_persistent_var_fixed';
load_system(mdl);
open_system([mdl '/MooreChart']);

%%
% In the MATLAB function:
%
% * The initial value of the persistent variable |n| depends on the input
% |u|.
% * The |return| statement interrupts the normal control flow of the
% function. 
%
% Because the chart implements Moore semantics, the initialization results
% in an error.
%
% If you modify the function so it initializes |n| independently of the
% input, then you can simulate an error-free model. 

sim(mdl);
open_system([mdl '/Scope']);