%% Vector Operation Optimization
% This example shows how Simulink&reg; Coder&trade; optimizes generated code by setting
% block output that generates vectors to scalars, for blocks such as the Mux, Sum, Gain, and
% Bus. This optimization reduces stack memory by replacing
% temporary local arrays with local variables.

% Copyright 2014 The MathWorks, Inc.


%% Example Model
% In the model, <matlab:rtwdemo_VectorOptimization rtwdemo_VectorOptimization>,
% the output of Gain blocks |G1| and |G2| are the vector signals |tmp1| and |tmp2|. These vectors have a width of 10.
model = 'rtwdemo_VectorOptimization';
open_system(model);
set_param(model, 'SimulationCommand', 'update')

%% 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)

%%
% The optimized code is in |rtwdemo_VectorOptimization.c|. The signals
% |tmp1| and |tmp2| are the local variables |rtb_tmp1| and |rtb_tmp2|.

cfile = fullfile(cgDir,'rtwdemo_VectorOptimization_grt_rtw',...
    'rtwdemo_VectorOptimization.c');
rtwdemodbtype(cfile,'/* Model step', '/* Model initialize', 1, 0);

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