%% Structures of Parameters
% Create a structure in the generated code. The structure stores parameter
% data.  
%% C Construct
% <include>CreateTunableParamStruct.c</include>
%% Procedure
% 1. Open the example model <matlab:rtwdemo_paraminline |rtwdemo_paraminline|>. 
open_system('rtwdemo_paraminline');
%%
% 2. On the *Modeling* tab, click *Model Data Editor*. In the Model Data Editor, select 
%    the *Parameters* tab.
%
% 3. In the model, click the Gain block labeled |G1|. In the Model Data 
%    Editor, use the *Value* column to set the value of the *Gain* parameter 
%    to |myStruct.G1|.
set_param('rtwdemo_paraminline/G1','Gain','myStruct.G1')

%%
% 4. Set the value of the *Gain* parameter in the |G2| block to |myStruct.G2|.
set_param('rtwdemo_paraminline/G2','Gain','myStruct.G2')

%%
% 5. Next to |myStruct.G2|, click the action button (with three vertical
%    dots) and select *Create*. 
%
% 6. In the Create New Data dialog box, set *Value* to |Simulink.Parameter(struct)| 
%    and click *Create*. A |Simulink.Parameter| object named |myStruct| 
%    appears in the base workspace.
%
% 7. In the Simulink.Parameter property dialog box, next to the *Value*
%    property, click the action button and select *Open Variable Editor*.
%
% 8. Right-click the white space under the *Field* column and select *New*. 
%    Name the new structure field |G1|. Use the *Value* column to set the 
%    value of the field to |2|.
myStruct.G1 = 2;

%%
% 9. Add a field |G2| whose value is |-2|, and then close the Variable Editor.
myStruct.G2 = -2;
%%
% 10. In the Simulink.Parameter property dialog box, on the *Code Generation* 
%     tab, set *Storage class* to |ExportedGlobal|. The structure |myStruct| 
%     appears in the generated code as a global variable.     
myStruct = Simulink.Parameter(myStruct);
myStruct.CoderInfo.StorageClass = 'ExportedGlobal';

%%
% 11. Generate code from the model.
evalc('rtwbuild(''rtwdemo_paraminline'')');

%% Results
% The generated header file |rtwdemo_paraminline_types.h| defines a structure 
% type that has a random name.
file = fullfile('rtwdemo_paraminline_grt_rtw',...
    'rtwdemo_paraminline_types.h');
rtwdemodbtype(file,'typedef struct {','} struct_6h72eH5WFuEIyQr5YrdGuB;',...
    1,1)
%%
% The source file |rtwdemo_paraminline.c| defines and initializes the
% structure variable |myStruct|.
file = fullfile('rtwdemo_paraminline_grt_rtw','rtwdemo_paraminline.c');
rtwdemodbtype(file,'/* Exported block parameters */',...
    '* External inputs (root inport signals with default storage) */',1,1)
%% Specify Name of Structure Type
% 1. Optionally, specify a name to use for the structure type definition
%    (|struct|). At the command prompt, use the function |Simulink.Bus.createObject| 
%    to create a |Simulink.Bus| object that represents the structure type. 
Simulink.Bus.createObject(myStruct.Value);
%%
% 2. The default name of the object is |slBus1|. Change the name by copying the
%    object into a new MATLAB variable.
myStructType = copy(slBus1);
%%
% 3. In the Model Data Editor, click the *Show/refresh additional information* 
%    button.
%
% 4. In the data table, find the row that corresponds to |myStruct|. Use the
%    *Data Type* column to set the data type of |myStruct| to |Bus:
%    myStructType|.
myStruct.DataType = 'Bus: myStructType';
%%
% 5. Generate code from the model.
evalc('rtwbuild(''rtwdemo_paraminline'')');
%%
% The code generates the definition of the structure type |myStructType|
% and uses this type to define the global variable |myStruct|.
rtwdemodbtype(file,'myStructType myStruct = {','/* Variable: myStruct',...
    1,1)

%% 
% Copyright 2020 The MathWorks, Inc.