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

    %% Optimize Generated Code Using Boolean Data for Logical Signals
% Optimize generated code by storing logical
% signals as Boolean data. 
% When you select the model configuration parameter *Implement logic
% signals as Boolean data (vs. double)*, blocks that generate logic signals
% output Boolean signals.
%
% The optimization:
%
% * Reduces the ROM and RAM consumption.
% * Improves execution speed.


%% Example Model
% Consider the model <matlab:rtwdemo_logicalAsBoolean rtwdemo_logicalAsBoolean>. 
% The outputs of the |Relational Operator|, |Logical Operator| and |HitCrossing| blocks are |double|, 
% even though they represent logical data.
model = 'rtwdemo_logicalAsBoolean';
open_system(model);

%% Generate Code
% 
% Create a temporary folder (in your system temporary folder) for the
% build and inspection process.
currentDir = pwd;
[~,cgDir] = rtwdemodir();

%%
% Build the model.
rtwbuild(model)

%%
% View the generated code without the optimization. These lines of code are 
% in |rtwdemo_logicalAsBoolean.h|.
hfile = fullfile(cgDir,'rtwdemo_logicalAsBoolean_ert_rtw',...
    'rtwdemo_logicalAsBoolean.h');
rtwdemodbtype(hfile,'/* External outputs','/* Parameters (auto storage) */',1,0);

%% Enable Optimization
% # Open the Configuration Parameters dialog box.  
% # On the *All Parameters* tab, select 
%   *Implement logic signals as Boolean data (vs. double)*.
%
% Alternatively, you can use the command-line API to enable the optimization:
set_param(model,'BooleanDataType','on');

%% Generate Code with Optimization
% The generated code stores the logical signal output as Boolean data.
%
%%
% Build the model.
rtwbuild(model)

%%
% View the generated code with the optimization. These lines of code are 
% in |rtwdemo_logicalAsBoolean.h|.
rtwdemodbtype(hfile,'/* External outputs','/* Parameters (auto storage) */',1,0);

%%
% Close the model and code generation report.
bdclose(model)
rtwdemoclean;
cd(currentDir)