%% Nested Structures of Signals
% You can create nested structures of signal data in the generated code.
%% C Construct
type('c_const_struct_for_nested_sig.c')
%% Procedure
% To represent a structure type in a model, create a |Simulink.Bus| object.
% Use the object as the data type of bus signals in your model.
%
% To nest a structure inside another structure, use a bus object as the
% data type of a signal element in another bus object.
%
% 1. Create the |ex_signal_nested_struct| 
%    model with Gain blocks, Bus Creator
%    blocks, and a Unit Delay block. The Gain and Unit Delay blocks make the
%    structure more identifiable in the generated code.
open_system('ex_signal_nested_struct_start')
%%
% 2. To configure a Bus Creator block to accept three inputs, in the block
%    dialog box, set *Number of inputs* to |3|.
% 
% 3. In the toolstrip, on the *Modeling* tab, under *Design*, click *Bus Editor*.
%
% In the Bus Editor, click *File* > *Add Bus* to create a |Simulink.Bus| 
% object and name it |A_struct_type|. Click *File* > *Add/Insert
% BusElement* to add two signal elements: |subStruct_B| and |subStruct_C|.
% For more information, see <docid:simulink_ug.brin2jr-1 Create Bus Objects 
% with the Bus Editor>. This bus object represents the top-level structure  
% type that you want the generated code to use.
%
% 4. Similarly, create |Simulink.Bus| objects |B_struct_type| (with three
% signal elements) and |C_struct_type| (with two signal elements).
%
% 5. In |A_struct_type| object, for the |subStruct_B| element, set *DataType* 
%    to |Bus: B_struct_type| and |subStruct_C| to |Bus: C_struct_type|.
%
% <<../A_struct_type.png>>
%
% Each signal element in |A_struct_type| uses another bus object as a data
% type. Now, these elements represent substructures.
%
% <<../B_C_struct_type.png>>
%
% 6. In the dialog box of the Bus Creator block that collects the three Gain
%    signals, set *Output data type* to |Bus: B_struct_type|. Click *Apply*.
%
% 7. Select *Output as nonvirtual bus* and click *OK*.
%
% 8. In the dialog box of the other subordinate Bus Creator block, set *Output
%    data type* to |Bus: C_struct_type| and select *Output as nonvirtual bus*.
%    Click *OK*.
%
% 9. In the last Bus Creator block dialog box, set *Output data type* to |Bus:
%    A_struct_type| and select *Output as nonvirtual bus*. Click *OK*.
%
% 10. Open the *Simulink Coder* app. In the *C Code* tab, select *Code
% Interface* > *Individual Element Code Mappings*.
%
% 11. Open the *Signals/States* tab. In the model, select the output signal 
%     of the |A_struct_type| Bus Creator block, which feeds the Unit Delay block.
%     Click the *Add selected signals to code mappings* button in the Code
%     Mappings editor.
%
% 12. For the added signal, set *Storage Class* to |ExportedGlobal|. 
%
% 13. In the Property Inspector, set the *Code* > *Identifier* property to
%     |sig_struct_var|. The output of the Bus Creator block appears in the
%     generated code as a separate global structure variable named
%     |sig_struct_var|.
%
% 14. Generate code from the model.
load_system('ex_signal_nested_struct')
evalc('rtwbuild(''ex_signal_nested_struct'')');
%% Results
% The generated header file |ex_signal_nested_struct_types.h| defines the
% structure types. Each structure type corresponds to a |Simulink.Bus|
% object.
file = fullfile('ex_signal_nested_struct_grt_rtw','ex_signal_nested_struct_types.h');
rtwdemodbtype(file,'#define DEFINED_TYPEDEF_FOR_B_struct_type_','} B_struct_type;',0,1)
rtwdemodbtype(file,'#define DEFINED_TYPEDEF_FOR_C_struct_type_','} C_struct_type;',0,1)
rtwdemodbtype(file,'#define DEFINED_TYPEDEF_FOR_A_struct_type_','} A_struct_type;',0,1)
%%
% The generated source file |ex_signal_nested_struct.c| allocates memory
% for the global structure variable |sig_struct_var|. By default, the name
% of the |A_struct_type| Bus Creator block is |Bus Creator2|.
file = fullfile('ex_signal_nested_struct_grt_rtw','ex_signal_nested_struct.c');
rtwdemodbtype(file,'/* Exported block signals */','A_struct_type sig_struct_var;',1,1)
%%
% In the same file, in the model |step| function, the algorithm accesses
% |sig_struct_var| and the fields of |sig_struct_var|.

%% 
% Copyright 2018 The MathWorks, Inc.