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

    %% Remove Code That Guards Against Division Exceptions for Integers and Fixed-Point Data
% Optimize generated code by removing code that protects
% against division by zero and overflows in division |INT_MIN/-1| operations for 
% integers and fixed-point data. If you are sure that these arithmetic exceptions 
% do not occur during program execution, enable this optimization. 
%  
% This optimization:
% 
% * Increases execution speed.
% * Reduces ROM consumption.
%
% *NOTE:* If you enable this optimization, it is possible that simulation
% results and results from generated code are not in bit-for-bit
% agreement. This example requires an Embedded Coder® license.


%   Copyright 2014 The MathWorks, Inc.

%% Example Model
% In the model <matlab:rtwdemo_nzcheck rtwdemo_nzcheck>, two
% signals of type |int8| feed into a divide block.
model = 'rtwdemo_nzcheck';
open_system(model);


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

%%
% Build the model.
set_param(model, 'NoFixptDivByZeroProtection', 'off');
rtwbuild(model);

%%
% View the generated code without the optimization. Here is a portion of |rtwdemo_nzcheck.c|.
cfile = fullfile(cgDir,'rtwdemo_nzcheck_ert_rtw','rtwdemo_nzcheck.c');
rtwdemodbtype(cfile,'/* Real-time model','/* Model step function',1, 1);

%% Enable Optimization
% # Open the Configuration Parameters dialog box.  
% # On the *Optimization* pane, select  *Remove code that protects 
% against division arithmetic exceptions*.
%
% Alternatively, you may use the command-line API to enable the optimization:
set_param(model, 'NoFixptDivByZeroProtection', 'on');

%% Generate Code with Optimization
% The optimized code does not contain code that checks for whether or not the
% divisor has a value of zero.


%%
% Build the model.
rtwbuild(model);

%%
% The following is a portion of |rtwdemo_nzcheck.c|. The code that protects
% against division arithmetic exceptions is not in the generated code.
rtwdemodbtype(cfile,'/* Real-time model','/* Model step function',1, 1);

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