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

    %% Use single Data Type as Default for Underspecified Types
% This example shows how to avoid introducing a double-precision
% data type in code generated for a single-precision hardware target.
%
% If you specify an inherited data type for signals, but
% data type propagation rules cannot determine data types for the signals,
% the signal data types default to |double|. You can use a model
% configuration parameter to specify the default data type as |single|.
%
% Copyright 2014 The MathWorks, Inc.

%% Explore Example Model
% Open the example model <matlab:rtwdemo_underspecified_datatype rtwdemo_underspecified_datatype>.
model = 'rtwdemo_underspecified_datatype';
open_system(model);
%%
% The root inports |In2|, |In3|, and |In4| specify
% |Inherit: Auto| for the *Data type* block parameter. The downstream blocks also use
% inherited data types.

%% Generate Code with |double| as Default Data Type 
% Create a temporary folder to contain the build files and folders.
currentDir = pwd;
[~,cgDir] = rtwdemodir();

%%
% Build the model using Embedded Coder.
rtwbuild(model)

%%
% In the code generation report, view the file
% |rtwdemo_underspecified_datatype.h|. The code uses the |double| data type
% to define the variables |In2|, |In3|, and |In4| because the Inport
% data types are underspecified in the model.
cfile = fullfile(cgDir,'rtwdemo_underspecified_datatype_ert_rtw',...
    'rtwdemo_underspecified_datatype.h');
rtwdemodbtype(cfile,...
    '/* External inputs (root inport signals with auto storage) */',...
    '/* External outputs (root outports fed by signals with auto storage) */', 1, 0);

%% Generate Code with |single| as Default Data Type
% Open the Configuration Parameters dialog box. 
% On the *Optimization* pane, select |single| in the *Default
% for underspecified data type* drop-down list.
%
% Alternatively, enable the optimization at the command prompt.
set_param(model, 'DefaultUnderspecifiedDataType', 'single');

%%
% Build the model using Embedded Coder.
rtwbuild(model)

%%
% In the code generation report, view the file
% |rtwdemo_underspecified_datatype.h|. The code uses the |single| data type
% to define the variables |In2|, |In3|, and |In4|.
rtwdemodbtype(cfile,...
    '/* External inputs (root inport signals with auto storage) */',...
    '/* External outputs (root outports fed by signals with auto storage) */', 1, 0);

%%
% Close the model and delete build files.
bdclose(model)
rtwdemoclean;
cd(currentDir)