www.gusucode.com > simulinkcoder 案例源码程序 matlab代码 > simulinkcoder/UseParameterObjectInDifferentDataTypeContextsExample.m
%% Reuse Parameter Data in Different Data Type Contexts % When % you use a |Simulink.Parameter| object or a numeric MATLAB variable to set two or more block parameter values, if % the block parameters have different data types, you must explicitly % specify the data type of the % object or variable. For example, you cannot leave the data type of the parameter object at the % default value, |auto|. %% Create and Configure Example Model % Create the model |ex_paramdt_contexts|. ex_paramdt_contexts %% % Set these block parameter values: % % * |In1| block: *Data type*: |single| % * |In2| block: *Data type*: |int8| % % In the block labeled |Gain - single|, set these parameter values: % % * *Gain*: |myGainParam| % * *Output data type*: |Inherit: Same as input| % * *Parameter data type*: |Inherit: Same as input| % % In the block labeled |Gain - int8|, set these parameter values: % % * *Gain*: |myGainParam| % * *Output data type*: |Inherit: Same as input| % * *Parameter data type*: |Inherit: Same as input| % % You can use these commands to set the parameter values: set_param('ex_paramdt_contexts/In1','OutDataTypeStr','single') set_param('ex_paramdt_contexts/In2','OutDataTypeStr','int8') set_param('ex_paramdt_contexts/Gain - single','Gain','myGainParam',... 'OutDataTypeStr','Inherit: Same as input',... 'ParamDataTypeStr','Inherit: Same as input') set_param('ex_paramdt_contexts/Gain - int8','Gain','myGainParam',... 'OutDataTypeStr','Inherit: Same as input',... 'ParamDataTypeStr','Inherit: Same as input') %% % Create a |Simulink.Parameter| object named |myGainParam| in the base % workspace. Configure % the object to appear in the generated code as a global variable by % applying the storage class |ExportedGlobal|. Configure the object to use the data type |int8|. myGainParam = Simulink.Parameter(3); myGainParam.CoderInfo.StorageClass = 'ExportedGlobal'; myGainParam.DataType = 'int8'; %% % In this model, you use the parameter object |myGainParam| to set two block % parameter values. The block parameters inherit different data types from the input signals (|single| or |int8|). % To use |myGainParam| in these different data type contexts, explicitly % specify % the data type of the parameter object by setting the |DataType| property % to |int8|. %% Match Parameter Object Data Type with Signal Data Type % Optionally, use a |Simulink.NumericType| or |Simulink.AliasType| object % to set the parameter object data type and one of the signal data types. This technique eliminates % unnecessary typecasts and shifts in the generated code due to a mismatch % between the parameter object data type and the signal data type. % % Create a |Simulink.NumericType| object to represent the data type |int8|. sharedType_int8 = fixdt('int8'); %% % Use this data type object to set: % % * The data type of the parameter object. Set the |DataType| % property to |sharedType_int8|. % * The data type of the |int8| signal. Use the *Data type* % parameter in the Inport block dialog box. % myGainParam.DataType = 'sharedType_int8'; set_param('ex_paramdt_contexts/In2','OutDataTypeStr','sharedType_int8') %% % The parameter object and the signal use the data type |int8|. To % change this data type, adjust the properties of the data % type object |sharedType_int8|. %% Generate and Inspect Code % Generate code from the model. rtwbuild('ex_paramdt_contexts') %% % The generated file |ex_paramdt_contexts.c| defines the global variable % |myGainParam| by using the data type |int8_T|, which corresponds to the data % type |int8| in Simulink. file = fullfile('ex_paramdt_contexts_grt_rtw','ex_paramdt_contexts.c'); rtwdemodbtype(file,'/* Exported block parameters */','int8_T myGainParam = 3;',1,1) %% % The generated code algorithm in the model |step| function uses |myGainParam| to % calculate the outputs of the two Gain blocks. In the case of the Gain % block whose input signal uses the data type |single|, the code algorithm casts % |myGainParam| to the data type |real32_T|, which corresponds to the data type % |single| in Simulink. rtwdemodbtype(file,'/* Model step function */',... '/* Model initialize function */',1,0)