%% Remove Zero Initialization Code for Internal Data
% This example shows how to eliminate code that initializes internal
% data to zero. 
%
%   Copyright 2009-2014 The MathWorks, Inc.


%% Example Model
% Open the model <matlab:rtwdemo_internal_init rtwdemo_internal_init>.
% The model contains an enabled subsystem whose initial output is zero.
% The subsystem contains a Unit Delay block whose initial condition is |0|.
model = 'rtwdemo_internal_init'; 
open_system(model);

%% Generate Code Without Optimization
% Build the model by using Embedded Coder. 
currentDir = pwd;
[~,cgDir] = rtwdemodir();
evalc('rtwbuild(model)');
%%
% This code is in the |rtwdemo_internal_init.c| file.
cfile = fullfile(cgDir,'rtwdemo_internal_init_ert_rtw','rtwdemo_internal_init.c');
rtwdemodbtype(cfile,'/* Model initialize', '* File trailer', 1, 0);

%% Enable Optimization
% Open the Configuration Parameters dialog box. On the *Optimization* pane, 
% select *Remove internal data zero initialization*.
%
% Alternatively, you can use the command prompt to enable the
% optimization. Set the model parameter 
% |ZeroInternalMemoryAtStartup| to |'off'|. 
%
%   set_param(model, 'ZeroInternalMemoryAtStartup', 'off');
set_param(model, 'ZeroInternalMemoryAtStartup', 'off');

%% Generate Code with Optimization
% Build the model by using Embedded Coder. 
evalc('rtwbuild(model)');
%%
% This code is in the |rtwdemo_internal_init.c| file.
% The generated code does not initialize internal data by assignment to zero.
rtwdemodbtype(cfile,'/* Model initialize', '* File trailer', 1, 0);

%%
bdclose(model)
rtwdemoclean;
cd(currentDir)


