%% Eliminate Unnecessary Typecasts and Shifts by Matching Data Types
% These examples show how to generate efficient code by configuring a block parameter to use the same data type as a signal that the block operates on.
%% Store Data Type Information in Model
% Open the example model rtwdemo_configrpinterface and configure it to show the generated names of blocks.

open_system('rtwdemo_configrpinterface')
set_param('rtwdemo_configrpinterface','HideAutomaticNames','off')

%
% On the Modeling tab, click Model Data Editor.
% In the Model Data Editor, select the Parameters tab.
% In the model, select the Gain block. In the Model Data Editor, the Data Type column shows that the data type of the Gain parameter of the block is set to Inherit: Same as input. With this setting, the Gain parameter of this block uses the same data type as the input signal.
% In the Code Mappings editor, under Parameters, set the storage class for K1 to ExportedGlobal.
% In the Model Data Editor, set Change view to Design.
% In the data table, for the row that represents K1, in the Data Type column, select Auto. With this setting, the parameter object acquires its data type from the block parameters that use the object (in this case, the Gain block parameter).
% Alternatively, to create and configure the object, use these commands at the command prompt:

cm = coder.mapping.api.get('rtwdemo_configrpinterface');
setModelParameter(cm,'K1','StorageClass','ExportedGlobal');
hws = get_param(bdroot, 'modelworkspace');
hws.setVariablePart('K1.DataType','Auto');

%
% Generate code from the model.

rtwbuild('rtwdemo_configrpinterface')

%
% The generated file rtwdemo_configrpinterface.c defines the global variable K1 by using the data type real_T.

file = fullfile('rtwdemo_configrpinterface_grt_rtw','rtwdemo_configrpinterface.c');
rtwdemodbtype(file,'/* Exported block parameters */','real_T K1 = 2.0;',1,1)

%
% The generated code algorithm in the model step function uses K1 directly without typecasting.

rtwdemodbtype(file,'output =',...
    '10U',1,1)

% In the Code Mappings editor, under Parameters, you can optionally set the storage class for K1 to Inherit: Inherit via internal rule (the default).
% In this case, the block parameter chooses the same data type as the input signal, double. However, when you use Inherit: Inherit via internal rule, under other circumstances (for example, when you use fixed-point data types), the block parameter might choose a different data type.
%
%% Store Data Type Information in Parameter Object
% When you use a Simulink.Parameter object to export or import parameter data from the generated code to your external code, for example by applying the storage class ImportedExtern, you can specify data type information in the parameter object. To match the data type of the parameter object with a signal data type, create a Simulink.NumericType or Simulink.AliasType object. You can strictly control the data type that the parameter object uses in the generated code, eliminating the risk that Simulink chooses a different data type when you make changes to the model.
% At the command prompt, create a Simulink.NumericType object that represents the data type double.

myType = Simulink.NumericType;
myType.DataTypeMode = 'Double';

%
% Use the Model Data Editor Data Type column to set these data types to myType:
% The parameter object. Use the Parameters tab.
% The Inport block named In2. Use the Inports/Outports tab. Due to data type propagation, the input signal of the Gain block also uses myType.
% Use the Model Data Editor Parameters tab to set the data type of the Gain block parameter to Inherit: Inherit from 'Gain'.
% Use this data type object as the data type of the parameter object.
% Alternatively, to configure the object and the blocks, use these commands at the command prompt:

K1.DataType = 'myType';
set_param('rtwdemo_configrpinterface/In2','OutDataTypeStr','myType')
set_param('rtwdemo_configrpinterface/Gain','ParamDataTypeStr','Inherit: Inherit from ''Gain''')

%
% Generate code from the model.

rtwbuild('rtwdemo_configrpinterface')

%
% The global variable K1 continues to use the data type real_T.

file = fullfile('rtwdemo_configrpinterface_grt_rtw','rtwdemo_configrpinterface.c');
rtwdemodbtype(file,'/* Exported block parameters */','real_T K1 = 2.0;',1,1)

% Close the code generation report.

rtwdemoclean;

% Copyright 2012 The MathWorks, Inc.