%% Enumeration
% To generate an enumerated data type, define an enumeration class in a
% MATLAB file. Then, use the enumeration class as the data type of signals,
% block parameters, and states in your model. 
%% C Construct
% <include>c_construct_enum.txt</include>
%% Procedure
% In your current folder, create the MATLAB file |ex_myEnumType.m|. The
% file defines an enumeration class |ex_myEnumType|. 
%
% <include>ex_myEnumType.m</include>
%
% 1. Open the model <matlab:ex_pattern_enum |ex_pattern_enum|> that has an 
%    Enumerated Constant block and a Multiport Switch block.
open_system('ex_pattern_enum')
%%
% 2. In the Model Data Editor, select the *Parameters* tab.
%
% 3. In the model, click the Enumerated Constant block.
%
% 4. In the Model Data Editor, use the *Value* column to set the constant
%    value to |myChoice|. Next to |myChoice|, click the action button 
%    (with three vertical dots) and select *Create*.
%
% 5. In the Create New Data dialog box, set *Value* to
%    |Simulink.Parameter(ex_myEnumType.Choice1)| and click *Create*. A
%    |Simulink.Parameter| object named |myChoice| appears in the base workspace.
%    The object stores the enumerated value |Choice1| of the type
%    |ex_myEnumType|.
%
% 6. In the property dialog box for |myChoice|, set *Storage Class* to
%    |ExportedGlobal|. With this setting, the object appears in the generated
%    code as a global variable.
myChoice = Simulink.Parameter(ex_myEnumType.Choice1);
myChoice.CoderInfo.StorageClass = 'ExportedGlobal';
%%
% 7. In the Model Data Editor, select the *Signals* tab.
%
% 8. In the model, select the output signal of the Enumerated Constant block.
%
% 9. In the Model Data Editor, use the *Data Type* column to set the signal
%     data type to |Enum: ex_myEnumType|.
%
% 10. Select the Multiport Switch block.
%
% 11. In the Property Inspector, set:
%
% * *Data port order* to |Specify indices|.
% * *Data port indices* to |enumeration('ex_myEnumType')|. This expression
% returns all of the enumeration members of |ex_myEnumType|.
%
% 12. Set the model configuration parameter *File packaging format* to 
%     |Modular|. With this setting, in the generated code, the definition 
%     of |ex_myEnumType| can appear in the specified header file, |myEnumHdr.h|.
%
% 13. To build the model and generate code, press *Ctrl+B* .
evalc('rtwbuild(''ex_pattern_enum'')');
%% Results
% View the generated header file |myEnumHdr.h|. The file defines the enumerated
% data type.
file = fullfile('ex_pattern_enum_ert_rtw','myEnumHdr.h');
rtwdemodbtype(file,'typedef enum {','} ex_myEnumType;',1,1)
%%
% View the source file |ex_pattern_enum.c|. The file
% defines the variable |myChoice|. The algorithm in the |step| function uses |myChoice| to
% route one of the input signals to the output signal.
file = fullfile('ex_pattern_enum_ert_rtw','ex_pattern_enum.c');
rtwdemodbtype(file,'ex_myEnumType myChoice = Choice1;','/* Variable: myChoice',1,1)
rtwdemodbtype(file,'/* Model step function */','/* Model initialize function */',1,0)

%% 
% Copyright 2019 The MathWorks, Inc.