%% 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
% 1. 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.
%
% 2. 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')
%%
% 3. To configure the Bus Creator block to accept three inputs, in the block
%    dialog box, set *Number of inputs* to |3|.
%
% 4. In the toolstrip, on the *Modeling* tab, under *Design*, click *Bus Editor*.
%
% 5. Use the Bus Editor to create a |Simulink.Bus| object named
%    |my_signals_type| that contains three signal elements: |signal1|,
%    |signal2|, and |signal3|. See <docid:simulink_ug#brin2jr-1 Create Bus 
%    Objects with the Bus Editor>.
%
% <<../my_signals_flat_busobj.png>>
%
% 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. On the *Modeling* tab, click *Model Data Editor*. In the Model Data Editor, 
%    on the *Signals* tab, from the *Change view* drop-down list, select |Code|.
%
% 9. In the model, click the output signal of the Bus Creator block.
%
% 10. In the Model Data Editor, for the output of the Bus Creator block, set
%     *Name* to |sig_struct_var|.
%
% 11. Set *Storage Class* to |ExportedGlobal|. The output of the Bus Creator 
%     block appears in the generated code as a separate global structure 
%     variable named |sig_struct_var|.
%
% 12. 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 2012 The MathWorks, Inc.