www.gusucode.com > simulinkcoder 案例源码程序 matlab代码 > simulinkcoder/EfficientCodeByMatchingDataTypesExample.m

    %% 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_basicsc|.
rtwdemo_basicsc
%%
% Open the |Gain| block dialog box. The input signal of this block uses the
% data type |single|.
%
% View the *Parameter Attributes* tab. *Parameter data type* is set to
% |Inherit: Same as input|. The *Gain* parameter of this block uses the
% same data type as the input signal.
%
% At the command prompt, convert the numeric variable |K1| to a
% |Simulink.Parameter| object.
K1 = Simulink.Parameter(2);
%%
% Configure |K1| to appear in the generated code as a global variable by
% applying the storage class |ExportedGlobal|.
K1.CoderInfo.StorageClass = 'ExportedGlobal';
%%
% On the *Main* tab, right-click the value of the *Gain* parameter, |K1|.
% Select *Open Variable*. The value of the *Data type* property is |auto|,
% which means the parameter object acquires its data type from the block
% parameters that use the object.
%
% Generate code from the model.
rtwbuild('rtwdemo_basicsc')
%%
% The generated file |rtwdemo_basicsc.c| defines the global variable |K1|
% by using the data type |real32_T|, which corresponds to the data type
% |single| in Simulink.
file = fullfile('rtwdemo_basicsc_grt_rtw','rtwdemo_basicsc.c');
rtwdemodbtype(file,'/* Exported block parameters */','real32_T K1 = 2.0F;',1,1)
%%
% The generated code algorithm in the model |step| function uses |K1|
% directly without typecasting.
rtwdemodbtype(file,' rtwdemo_basicsc_DW.X = K1',...
    ' rtCP_Table1_bp01Data, rtCP_Table1_tableData,',1,1)
%%
% In the *Gain* block dialog box, on the *Parameter Attributes* tab, you
% can optionally set *Parameter data type* to |Inherit: Inherit via
% internal rule| (the default). In this case, the block parameter
% chooses the same data type as the input signal (|single|). 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 custom 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 |single|.
myType = Simulink.NumericType;
myType.DataTypeMode = 'Single';
%%
% Use this data type object as the data type of the parameter object.
K1.DataType = 'myType';
%%
% Use the data type object to set the output data type of the Inport block
% named |In2|. Due to data type propagation, the input signal of the Gain
% block also uses this data type.
set_param('rtwdemo_basicsc/In2','OutDataTypeStr','myType')
%%
% In the Gain block dialog box, on the *Parameter Attributes* tab, set
% *Parameter data type* to |Inherit: Inherit from 'Gain'|.
set_param('rtwdemo_basicsc/Gain','ParamDataTypeStr','Inherit: Inherit from ''Gain''')
%%
% Generate code from the model.
rtwbuild('rtwdemo_basicsc')
%%
% The global variable |K1| continues to use the data type |real32_T|.
file = fullfile('rtwdemo_basicsc_grt_rtw','rtwdemo_basicsc.c');
rtwdemodbtype(file,'/* Exported block parameters */','real32_T K1 = 2.0F;',1,1)