%% Optimize Generated Code Using Inline Invariant Signals
% This example shows how to use inline invariant signals to optimize 
% the generated code. This optimization transforms symbolic names of invariant signals 
% into constant values.  
%
% The |InlineInvariantSignals| optimization: 
% 
% * Reduces ROM and RAM consumption.
% * Improves execution speed.
%% Example Model
% Consider the model <matlab:rtwdemo_inline_invariant_signals>.
model = 'rtwdemo_inline_invariant_signals';
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 using Simulink Coder.
rtwbuild(model)

%%
% View the generated code without the optimization. These lines of code are in |rtwdemo_inline_invariant_signals.c|.
cfile = fullfile(cgDir,'rtwdemo_inline_invariant_signals_grt_rtw',...
    'rtwdemo_inline_invariant_signals.c');
rtwdemodbtype(cfile,'/* Output and update for atomic system',...
    '/* Model output', 1, 0);

%% Enable Optimization
% # Open the Configuration Parameters dialog box.  
% # On the *Optimization* pane, select 
%   *Inline Invariant Signals*.
%
% Alternatively, you can use the command-line API to enable the optimization:
set_param(model, 'InlineInvariantSignals', 'on');

%% Generate Code with Optimization
% The generated code uses the numerical values of the folded constants instead of creating an
% additional structure (rtwdemo_inline_invariant_ConstB).
%
%%
% Build the model using Simulink Coder.
rtwbuild(model)

%%
% View the generated code with the optimization. These lines of code are in |rtwdemo_minmax.c|.
rtwdemodbtype(cfile,...
    '/* Output and update for atomic system', '/* Model output', 1, 0);

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

%% 
% Copyright 2012 The MathWorks, Inc.