%% Execute Standalone Chart in a Simulink Model
% You can execute a standalone Stateflow chart from within a Simulink
% model. For example, the model |sf_change_model| contains two Stateflow
% charts that use the standalone chart |sf_change| to count the number of
% ways to make change for 27 cents in two different currency systems. You
% can simulate the model, but the functions that execute the standalone
% chart do not support code generation. 

% Copyright 2018-2020 The MathWorks, Inc.

%% 

% Set up charts with white background
set_param(0,'ExportBackgroundColorMode','white');

model = 'sf_change_model';
open_system(model)

%%
% Each chart contains these states:
%
% * |Initialize| creates a local chart object |chartObj| that implements
% the change-counting algorithm for the input value |x|.
% 
% * |Execute| calls the |step| function to execute the standalone chart and
% stores the result as the output data |tally|.
% 
% * |Finish| displays the results of the algorithm in the Diagnostic Viewer
% window and sets the value of the output data |done| to |true|.
%
% When both charts reach their respective |Finish| state, the simulation of
% the model stops and the Display blocks show the final values of the two
% tallies.

%% Execution Using MATLAB as the Action Language
% The chart |MATLAB syntax| uses MATLAB as the action language. To execute
% the standalone Stateflow chart, this chart must follow these guidelines:
% 
% * The local variable |chartObj| that contains the handle to the chart
% object has type |Inherit: From definition in chart|.
% 
% * Before creating the chart object, the |Initialize| state calls the
% |coder.extrinsic| function to declare |sf_change| as an extrinsic
% function that is restricted for code generation in Simulink. See
% <docid:stateflow_ug#bvlxgo9>.
%
% * The |Execute| and |Finish| states access the local data for the
% standalone chart by calling the |get| function.

open_system([model '/MATLAB syntax'])

%%
% When you simulate this chart with an input of |x| = 27, the Display block
% |Olde English| shows a tally of 4. The Diagnostic Viewer window shows
% these results:
%
%     Pennies    Shillings    Florins
%     _______    _________    _______
%       27           0           0   
%       15           1           0   
%        3           2           0   
%        3           0           1   

%% Execution Using C as the Action Language
% The chart |C syntax| uses C as the action language. To execute the
% standalone Stateflow chart, this chart must follow these guidelines:
% 
% * The local variable |chartObj| that contains the handle to the chart
% object has type |ml|.
% 
% * The |Initialize| state calls the |ml| function to create the chart
% object.
%
% * The |Execute| and |Finish| states use the |ml| namespace operator to
% access the |step|, |get|, and |displ| functions to execute the standalone
% chart, access its local data, and display the results of the algorithm.
%
% For more information, see <docid:stateflow_ug#f0-59542>.

open_system([model '/C syntax'])

%% 
% When you simulate this chart with an input of |x| = 27, the Display block
% |Modern American| shows a tally of 13. The Diagnostic Viewer window shows
% these results:
% 
%     Safety    FieldGoal    TouchDown
%     ______    _________    _________
%       12          1            0    
%        9          3            0    
%        6          5            0    
%        3          7            0    
%        0          9            0    
%       10          0            1    
%        7          2            1    
%        4          4            1    
%        1          6            1    
%        5          1            2    
%        2          3            2    
%        3          0            3    
%        0          2            3
