%% Structures of Signals
% This example shows how to create a structure of signal data in the
% generated code. 
%% C Construct
type('c_const_struct_for_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.
%
% 1. Create the |ex_signal_struct| model by using 
%    Gain blocks, a Bus Creator block, and a Unit Delay block. The Gain and 
%    Unit Delay blocks make the structure more identifiable in the generated code.
open_system('ex_signal_struct_start')
%%
% 2. To configure the 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*.
%
% 4. In the Bus Editor, click *File* > *Add Bus* to create a |Simulink.Bus| 
%    object and name it |my_signals_type|. Click *File* > *Add/Insert BusElement*
%    to add three signal elements: |signal1|,
%    |signal2|, and |signal3|. For more information, see <docid:simulink_ug#brin2jr-1 
%    Create Bus Objects with the Bus Editor>.   
%
% <<../my_signals_flat_busobj.png>>
%
% 5. Save the bus objects in your current folder as
%    |ex_signal_struct_data.mat|.
%
% This bus object represents the structure type that you want the generated
% code to use.
%
% 6. In the Bus Creator block dialog box, set *Output data type* to |Bus:
%    my_signals_type|.
%
% 7. Select *Output as nonvirtual bus*. Click *OK*. A nonvirtual bus appears
%    in the generated code as a structure.
%
% 8. Open the *Simulink Coder* app. In the *C Code* tab, select *Code
%    Interface* > *Individual Element Code Mappings*.
%
% 9. Open the *Signals/States* tab. In the model, select the output signal of the 
%    Bus Creator block and click the *Add selected signals to code
%    mappings* button in the Code Mappings editor.
%
% 11. For the added signal, set *Storage Class* to |ExportedGlobal|. 
%
% 12. 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|.
%
% 13. Generate code from the model.
load_system('ex_signal_struct')
evalc('rtwbuild(''ex_signal_struct'')');
%% Results
% The generated header file |ex_signal_struct_types.h| defines the structure type
% |my_signals_type|.
file = fullfile('ex_signal_struct_grt_rtw','ex_signal_struct_types.h');
rtwdemodbtype(file,'#define DEFINED_TYPEDEF_FOR_my_signals_type_','} my_signals_type;',0,1)
%%
% The source file |ex_signal_struct.c| allocates memory for the global
% variable |sig_struct_var|, which represents the output of the Bus Creator
% block.
file = fullfile('ex_signal_struct_grt_rtw','ex_signal_struct.c');
rtwdemodbtype(file,'/* Exported block signals */','my_signals_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 2020 The MathWorks, Inc.