%% Generate Tunable Initial Condition Structure for Bus Signal
% When you use a MATLAB(R) structure to specify initialization
% values for the signal elements in a bus, you can create a tunable global
% structure in the generated code. 
%
% If you set *Configuration Parameters > Optimization > Default parameter behavior* to |Tunable|, the initial
% condition appears as a tunable substructure of the global parameters
% structure.  
%
% Whether you set *Default parameter behavior* to |Tunable| or |Inlined|,
% you can specify the initial condition by using a tunable |Simulink.Parameter| object whose
% value is a structure. If you apply a storage class other than |Auto| to
% the parameter object, the structure is tunable in the generated
% code.
%
% To generate efficient code by avoiding data type
% mismatches between the structure and the bus signal, use either:
%
% * Typed expressions to specify the values of the structure fields. Match the
% data type of each field with the data type of the
% corresponding signal element.
% * A |Simulink.Bus| object to control the data types of the structure
% fields and the signal elements.
%
% For basic information about using structures to initialize bus signals,
% and to decide how to control field data types,
% see <docid:simulink_ug.bsff8zh Specify Initial Conditions for Bus Signals>.
%% Generate Tunable Initial Condition Structure
% This example shows how to use a tunable structure parameter to initialize
% a virtual bus signal.
%
% Open the example model <matlab:open_system('rtwdemo_tunable_init_struct');set_param('rtwdemo_tunable_init_struct','HideAutomaticNames','off'); |rtwdemo_tunable_init_struct|> and configure it to show the generated names of blocks.
load_system('rtwdemo_tunable_init_struct')
set_param('rtwdemo_tunable_init_struct','SimulationCommand','Update',...
    'HideAutomaticNames','off')
open_system('rtwdemo_tunable_init_struct')
%%
% On the *Modeling* tab, click *Model Data Editor*.
% 
% On the *Inports/Outports* tab, the *Data Types* column shows that each
% Inport block in the model uses a different output data type.
%
% Open the Bus Creator block dialog box. The block output is a virtual bus.
%
% In the Configuration Parameters dialog box, open the *Optimization* pane. The configuration parameter *Default
% parameter behavior* is set to |Tunable|. By default, block parameters,
% including initial conditions, appear in the generated code as tunable fields of the global
% parameters structure.
%
% In the Model Data Editor, inspect the *States* tab.
%
% For the Unit Delay block, set *Initial Value* to a
% structure that specifies an initial condition for each of the three
% signal elements. To generate efficient code, match the data types of the
% structure fields with the data types of the corresponding signal
% elements. For example, set *Initial Value* to the expression
% |struct('thermocpl',15.23,'magFlow',uint32(79),'posSwitch',false)|.
%
%   set_param('rtwdemo_tunable_init_struct/Unit Delay','InitialCondition',...
%   'struct(''thermocpl'',15.23,''magFlow'',uint32(79),''posSwitch'',false)')
set_param('rtwdemo_tunable_init_struct/Unit Delay','InitialCondition',...
    'struct(''thermocpl'',15.23,''magFlow'',uint32(79),''posSwitch'',false)')
%%
% Generate code from the example model.
rtwbuild('rtwdemo_tunable_init_struct')
%%
% In the code generation report, view the file |rtwdemo_tunable_init_struct_types.h|.
% The code defines a structure type whose fields use the data
% types that you specified in the |struct| expression.
file = fullfile('rtwdemo_tunable_init_struct_grt_rtw','rtwdemo_tunable_init_struct_types.h');
rtwdemodbtype(file,'#ifndef DEFINED_TYPEDEF_FOR_struct_','}',1,1)
%%
% View the file |rtwdemo_tunable_init_struct.h|. The |struct| type definition of
% the global parameters structure contains a substructure,
% |UnitDelay_InitialCondition|, which represents the *Initial condition*
% parameter of the Unit Delay block.
file = fullfile('rtwdemo_tunable_init_struct_grt_rtw','rtwdemo_tunable_init_struct.h');
rtwdemodbtype(file,'struct P_rtwdemo_tunable_init_struct_T_ {','UnitDelay_InitialCondition;',1,1)
%%
% View the file |rtwdemo_tunable_init_struct_data.c|. This source file
% allocates memory for the global parameters structure. The substructure
% |UnitDelay_InitialCondition| appears.
file = fullfile('rtwdemo_tunable_init_struct_grt_rtw','rtwdemo_tunable_init_struct_data.c');
rtwdemodbtype(file,'/* Block parameters (default storage) */','}',1,1)
%%
% View the file |rtwdemo_tunable_init_struct.c|. The model initialization function uses the fields of the substructure to initialize the block states.
file = fullfile('rtwdemo_tunable_init_struct_grt_rtw','rtwdemo_tunable_init_struct.c');
rtwdemodbtype(file,'* InitializeConditions for UnitDelay generated from: ''<Root>/Unit Delay'' */',...
    'rtwdemo_tunable_init_struct_P.UnitDelay_InitialCondition.posSwitch',1,1)
%% Use Bus Object to Specify Data Types
% If you create a bus object, you can use it to specify the
% data type of the bus signal and the tunable initial condition structure. 
% Before code generation, the
% |Simulink.Parameter| object casts the values of the structure fields to the data types of the 
% signal elements. 
% For basic information about creating bus objects and using them
% in models, see <docid:simulink_ug.f15-109955 When to Use Bus Objects>.
%
% Open the example model <matlab:open_system('rtwdemo_init_struct_busobj');set_param('rtwdemo_init_struct_busobj','HideAutomaticNames','off'); |rtwdemo_init_struct_busobj|> and configure it to show the generated names of blocks.
open_system('rtwdemo_init_struct_busobj')
load_system('rtwdemo_init_struct_busobj')
set_param('rtwdemo_init_struct_busobj','SimulationCommand','Update',...
    'HideAutomaticNames','off')
open_system('rtwdemo_init_struct_busobj')
%%
% In the base workspace, double-click the |Simulink.Bus| object
% |ComponentData|. The object defines three signal elements: |thermocpl|,
% |magFlow|, and |posSwitch|. The elements each use a different data type.
%
% In the model, open the Model Data Editor (on the *Modeling* tab, click *Model Data Editor*). The
% *Inports/Outports* tab shows that for the Inport block |DataIn|, the
% output data type (*Data Type* column) is set to |Bus: ComponentData|.
%
% At the command prompt, create the structure parameter |initStruct|. You can specify the
% field values by using untyped expressions. To improve readability, 
% specify the field |posSwitch| with a Boolean value. 
%
%   initStruct = struct(...
%       'thermocpl',15.23,...
%       'magFlow',79,...
%       'posSwitch',false ...
%       );
%   
%   initStruct = Simulink.Parameter(initStruct);

initStruct = struct(...
    'thermocpl',15.23,...
    'magFlow',79,...
    'posSwitch',false...
    );

initStruct = Simulink.Parameter(initStruct);
%%
% In the Model Data Editor, inspect the *Parameters* tab.
%
% In the model, click the Unit Delay block. The Model Data Editor
% highlights the row that corresponds to the *Initial condition* parameter
% of the block.
%
% In the Model Data Editor, set the parameter value (*Value* column) to |initStruct|.
set_param('rtwdemo_init_struct_busobj/Unit Delay','InitialCondition','initStruct')
%%
% Click the *Show/refresh additional information* button. The parameter object, |initStruct|, appears in the
% data table as a row.
%
% Use the *Data Type* column to set the data type of |initStruct| to |Bus:
% ComponentData|.
%
%   initStruct.DataType = 'Bus: ComponentData';
initStruct.DataType = 'Bus: ComponentData';
%%
% Set the *Change View* drop-down list to |Code|.
%
% Use the *Storage Class* column to apply the storage class
% |ExportedGlobal| to |initStruct|.
%
%   initStruct.StorageClass = 'ExportedGlobal';
initStruct.StorageClass = 'ExportedGlobal';
%%
% Generate code from the example model.
rtwbuild('rtwdemo_init_struct_busobj')
%%
% In the code generation report, view the file |rtwdemo_init_struct_busobj_types.h|.
% The code creates a structure type |ComponentData| whose fields use the data
% types in the bus object.
file = fullfile('rtwdemo_init_struct_busobj_grt_rtw','rtwdemo_init_struct_busobj_types.h');
rtwdemodbtype(file,'#ifndef DEFINED_TYPEDEF_FOR_ComponentData_','}',1,1)
%%
% View the file |rtwdemo_init_struct_busobj.c|. The code creates a global variable to represent the tunable
% parameter object |initStruct|.
file = fullfile('rtwdemo_init_struct_busobj_grt_rtw','rtwdemo_init_struct_busobj.c');
rtwdemodbtype(file,'/* Exported block parameters */','Variable: initStruct',1,1)
%%
% The model initialization function uses the structure fields to initialize the block states.
rtwdemodbtype(file,'/* InitializeConditions for UnitDelay generated from: ''<Root>/Unit Delay'' */',...
    '= initStruct.posSwitch;',1,1)
%%
% To change the data type of a signal element, specify the
% new type in the bus object. The signal element in the model uses
% the new type. Before simulation and code generation, the parameter object |initStruct| casts the
% corresponding structure field to the new type.

%% 
% Copyright 2012 The MathWorks, Inc.