%% Represent Block Parameter as Tunable Global Variable
% This example shows how to create tunable parameter data 
% by representing block parameters as global variables in the generated
% code. 
%% Configure Block Parameter by Using Parameter Object
% Open the example model <matlab:rtwdemo_paraminline;set_param('rtwdemo_paraminline','HideAutomaticNames','off'); |rtwdemo_paraminline|> and configure it to show the generated names of blocks.
load_system('rtwdemo_paraminline')
set_param('rtwdemo_paraminline','HideAutomaticNames','off')
open_system('rtwdemo_paraminline')
%%
% On the *Modeling* tab, click *Model Data Editor*.
%
% In the Model Data Editor, inspect the *Parameters* tab.
%
% In the model, click the |G1| Gain block. The Model Data Editor highlights
% the row that corresponds to the *Gain* parameter of the block.
%
% In the Model Data Editor *Value* column, change the gain value from |2|
% to |myGainParam|.
%
% Next to |myGainParam|, click the action button (with three vertical dots)
% and select *Create*.
%
% In the Create New Data block dialog box, set *Value* to
% |Simulink.Parameter(2)|. Click *Create*. A |Simulink.Parameter| object
% |myGainParam| stores the parameter value, |2|, in the model workspace.
%
% In the myGainParam dialog box, on the *Code Generation* tab, click *Configure in Coder App*.
%
% In the Code Mappings editor, set *Storage Class* of |myGainParam| to |ExportedGlobal|. This
% storage class causes the parameter object to appear in the generated code
% as a tunable global variable.
%
% Alternatively, to create the
% parameter object and configure the model, use these commands at the command prompt:
set_param('rtwdemo_paraminline/G1','Gain','myGainParam')
mws = get_param('rtwdemo_paraminline', 'modelworkspace');
mws.assignin('myGainParam',Simulink.Parameter(2));
cm = coder.mapping.utils.create('rtwdemo_paraminline');
setModelParameter(cm,'myGainParam','StorageClass','ExportedGlobal');
%%
% Use the Model Data Editor to create a parameter object, |myOtherGain|,
% for the |G2| Gain block. Apply the storage class |ExportedGlobal|.
%
% Alternatively, use these commands at the command prompt:
set_param('rtwdemo_paraminline/G2','Gain','myOtherGain')
mws.assignin('myOtherGain',Simulink.Parameter(-2));
setModelParameter(cm,'myOtherGain','StorageClass','ExportedGlobal');
%% Generate and Inspect Code
% Generate code from the model.
rtwbuild('rtwdemo_paraminline')
%%
% The generated file |rtwdemo_paraminline.h| contains |extern|
% declarations of the global variables |myGainParam| and |myOtherGain|. You can include (|#include|)
% this header file so that your code can read and write the value of the
% variable
% during execution.
file = fullfile('rtwdemo_paraminline_grt_rtw','rtwdemo_paraminline.h');
rtwdemodbtype(file,...
    'extern real_T myGainParam;','Referenced by: ''<Root>/G2''',1,1)
%%
% The file |rtwdemo_paraminline.c| allocates memory for and initializes
% |myGainParam| and |myOtherGain|.
file = fullfile('rtwdemo_paraminline_grt_rtw','rtwdemo_paraminline.c');
rtwdemodbtype(file,...
    '/* Exported block parameters */','Referenced by: ''<Root>/G2''',1,1)
%%
% The generated code algorithm in the model |step| function uses
% |myGainParam| and |myOtherGain|
% for calculations.
rtwdemodbtype(file,...
    '/* Model step function */','/* Model initialize function */',1,0)

%% 
% Copyright 2012 The MathWorks, Inc.