www.gusucode.com > ecoder 案例源码程序 matlab代码 > ecoder/CreateANamedFixedPointDataTypeInTheGeneratedCodeExample.m

    %% Create a Named Fixed-Point Data Type in the Generated Code
% This example shows how to create and name a fixed-point data type in
% generated code. You can use the name of the type to specify parameter and
% signal data types throughout a model and in generated code.
%
% Create a |Simulink.NumericType| object
% that defines a fixed-point data type. Name the object |myFixType|.
myFixType = fixdt(1,16,3)
%%
% Use the name of the object as an alias for the fixed-point type in models and in generated code.
myFixType.IsAlias = true;
%%
% Open the model |rtwdemo_paramdt|.
open_system('rtwdemo_paramdt')
%%
% The model creates a |Simulink.Parameter| object |Kuser| with
% value |8| in the base workspace. The model uses |Kuser| as
% a parameter in a Gain block.
%
% Set the data type of |Kuser| to the fixed-point data
% type.
Kuser.DataType = 'myFixType';
%%
% At the top level of the model, set the output data type of the Inport block
% labeled |7| to |myFixType|.
set_param('rtwdemo_paramdt/In7','OutDataTypeStr','myFixType')
%%
% Open the subsystem.
%
% Set the output data type of the Inport block labeled |7| to |myFixType|.
set_param('rtwdemo_paramdt/Subsystem/In7','OutDataTypeStr','myFixType')
%%
% At the top level of the model, double-click the blue button labeled *Generate
% Code Using Embedded Coder*.
rtwconfiguredemo(bdroot,'noop',false,{})
rtwbuilddemomodel(bdroot)
%%
% In the code generation report, view the file |rtwdemo_paramdt.h|.
% The code defines the type |myFixType| based on an
% integer type of the specified word length.
file = fullfile('rtwdemo_paramdt_ert_rtw','rtwdemo_paramdt.h');
rtwdemodbtype(file,'#ifndef DEFINED_TYPEDEF_FOR_myFixType_',...
    '#ifndef DEFINED_TYPEDEF_FOR_aliasType_',1,0)
%%
% View the file |rtwdemo_paramdt.c|. The code uses
% the type |myFixType|, which is an alias of the integer
% type |int16|, to define the variable |Kuser|.
file = fullfile('rtwdemo_paramdt_ert_rtw','rtwdemo_paramdt.c');
rtwdemodbtype(file,'myFixType Kuser = 64;','myFixType Kuser = 64;',1,1)
%%
% The stored integer value |64| of |Kuser| is
% not the same as the real-world value |8| because
% of the scaling that the fixed-point data type |myFixType| specifies.
% For more information, see <docid:fixedpoint_ug.f20784> 
% in the Fixed-Point Designer documentation.
%
% The line of code that represents the Gain block applies
% a right bit shift corresponding to the fraction length specified by |myFixType|.
rtwdemodbtype(file,'rtY.Out7 = (myFixType)(Kuser * rtU.In7 >> 3);',...
    'rtY.Out7 = (myFixType)(Kuser * rtU.In7 >> 3);',1,1)