%% Macro Definitions (#define)

% Copyright 2020 The MathWorks, Inc.

%% C Construct
% |#define myParam 9.8;| 

%% Export Generated Macro Definition
%
% 1. Open example model <matlab:ex_param_macro |ex_param_macro|>.
open_system('ex_param_macro');

%%
% 2. In the model, select the Gain block. In the Property Inspector, set 
%    the value of the *Gain* parameter to |myParam|.
%%
% 3. Next to the parameter value, click the action button (the button with 
%    three vertical dots) and select *Create*.
myParam = Simulink.Parameter(9.8);
set_param('ex_param_macro/Gain','Gain','myParam');
%%
% 4. In the Create New Data dialog box, set *Value* to |Simulink.Parameter(9.8)|. 
%    Click *Create*.
%    |A Simulink.Parameter| object, |myParam|, appears in the base workspace. 
%    The Gain block uses the object to set the value of the Gain parameter, 
%    in this case, |9.8|.
%
% 5. On the *Code Generation* tab, click the *Configure in Coder App* button. 
%    In the Code Mappings editor, set *Storage Class* to |Define|. Click *OK*.
myParam.StorageClass = 'Define';
%%
% 6. To build the model and generate code, press *Ctrl+B* .
evalc('rtwbuild(''ex_param_macro'')');
%%
% The generated header file |ex_param_macro.h| defines |myParam| as a macro.
file = fullfile('ex_param_macro_ert_rtw','ex_param_macro.h');
rtwdemodbtype(file,'/* Definition for custom storage class: Define */', ...
    '/* External inputs (root inport signals with default storage) */',1,1);

%% Reuse Macro from Handwritten Code
%
% 1. In the Code Mappings editor, on the *Parameters*
%    tab, click the *Update Code Mappings* button.
%
% 2. Change *Storage Class* of |myParam| from |Define| to |ImportedDefine|.
myParam.StorageClass = 'ImportedDefine';
%%
% 3. In the Property Inspector, under *Code* section, set *Header File*
%    to |external_params.h|. The 
%    generated code imports the macro definition from a custom header 
%    file named |external_params.h|.
myParam.CoderInfo.CustomAttributes.HeaderFile = 'external_params.h';
%%
% 4. In your current folder, create the C header file |external_params.h|,
%    which contains the |#define| statement.
%
% <include>external_params.h</include>

%%
% 5. To build the model and generate code, press *Ctrl+B* .
evalc('rtwbuild(''ex_param_macro'')');

%%
% The generated header file |ex_param_macro.h| does not define the 
% macro. Instead, the file includes |(#include)| the custom header file 
% |external_params.h|.
file = fullfile('ex_param_macro_ert_rtw','ex_param_macro.h');
rtwdemodbtype(file,'/* Includes for objects with custom storage classes. */', ...
    '#include "external_params.h"',1,1);

%%
% The source file |ex_param_macro.c| contains a guard to check that a 
% definition for |myParam| exists.
file = fullfile('ex_param_macro_ert_rtw','ex_param_macro.c');
rtwdemodbtype(file,'#include "ex_param_macro.h"', ...
  '/* External inputs (root inport signals with default storage) */',0,1);









