%% Configure Model from Command Line
% The code generator provides model configuration parameters 
% for customizing generated code. Depending on how you use and interact 
% with the generated code, 
% you make configuration decisions.  You choose a 
% configuration that best matches your needs for debugging,
% traceability, code efficiency, and safety precaution.
%
% It is common to automate the model
% configuration process by using a MATLAB(R) script once you have
% decided upon a desired 
% configuration. 
%
% The example describes:
%
% * Concepts of working with configuration parameters
% * Documentation to understand the code generation options
% * Tools and scripts to automate the configuration of a model
%
%% Configuration Parameter Workflows
% There are many workflows for Configuration Parameters that include
% persistence within a single model or persistence across multiple models.
% Depending on your needs, you can work with configuration sets as
% copies or references.  This example shows the basics steps for
% working directly with the active configuration set of a model.  For a
% comprehensive description of configuration set features and workflows, see
% <matlab:helpview(fullfile(docroot,'toolbox','simulink','helptargets.map'),'ModelConfig') Configuration Sets>
% in the Simulink(R) documentation.
%
%% Configuration Set Basics
% Load a model into memory.
model='rtwdemo_configwizard';
load_system(model)
%%
% Obtain the model's active configuration set.
cs = getActiveConfigSet(model);

%%
% Simulink(R) Coder(TM) exposes a subset of the code generation
% options. If you are using Simulink(R) Coder(TM), select the Generic
% Real-Time (GRT) target.
switchTarget(cs,'grt.tlc',[]);

%%
% Embedded Coder(R) exposes the complete set of code
% generation options.  If you are using Embedded Coder(R), select
% the Embedded Real-Time (ERT) target.
switchTarget(cs,'ert.tlc',[]);

%%
% To automate configuration of models built for 
% GRT- and ERT-based targets, the configuration set *|IsERTTarget|* 
% attribute is useful.
isERT = strcmp(get_param(cs,'IsERTTarget'),'on');
%%
% You can interact with code generation options via the model or 
% the configuration set.  This example gets and sets options indirectly
% via the model.
deftParamBehvr = get_param(model,'DefaultParameterBehavior');  % Get
set_param(model,'DefaultParameterBehavior',deftParamBehvr)     % Set
%%
% This example gets and sets options directly via the configuration set.
if isERT
    lifespan = get_param(cs,'LifeSpan');  % Get LifeSpan
    set_param(cs,'LifeSpan',lifespan)     % Set LifeSpan
end
%% Configuration Option Summary
% The full list of code generation options are documented with 
% tradeoffs for debugging, traceability, code efficiency, and safety
% precaution.
%
% * <matlab:helpview(fullfile(docroot,'toolbox','rtw','helptargets.map'),'rtw_param_ref') Simulink(R) Coder(TM) options>
% * <matlab:helpview(fullfile(docroot,'toolbox','ecoder','helptargets.map'),'ecoder_param_ref') Embedded Coder(R) options>
%
% Use Code Generation Advisor to obtain a model 
% configuration optimized for your goals.  In the Set Objectives dialog
% box, you can set and prioritize objectives.
%
% <<../rtw_objectives_selection_ert.jpg>>

%%
% You can find documentation about the
% <matlab:helpview(fullfile(docroot,'toolbox','rtw','helptargets.map'),'scoder_code_gen_advisor') Code Generation Advisor>
% in the Simulink Coder documentation and
% <matlab:helpview(fullfile(docroot,'toolbox','ecoder','helptargets.map'),'code_gen_advisor') additional documentation>
% specific to Embedded Coder(R).

%% Parameter Configuration Scripts
% Simulink(R) Coder(TM) provides an example configuration script that you
% can use as a starting point for your application.  A list of the most
% relevant GRT and ERT code generation options are contained in
% <matlab:edit('rtwconfiguremodel') rtwconfiguremodel.m>.
%
% Alternatively, you can generate a MATLAB function that
% contains the complete list of model configuration parameters by using the
% configuration set |saveAs| function.

% Go to a temporary writable directory.
currentDir = pwd;
rtwdemodir();

% Save the model's configuration parameters to file 'MyConfig.m'.
saveAs(cs,'MyConfig')

% Display the first 50 lines of MyConfig.m.
dbtype MyConfig 1:50

%%
% Each parameter setting in the generated file includes a comment for the
% corresponding parameter string in the Configuration Parameters dialog
% box.

% Return to previous working directory.
cd(currentDir)

%% Configuration Wizard Blocks
% Embedded Coder(R) provides a set of
% <matlab:helpview(fullfile(docroot,'toolbox','ecoder','helptargets.map'),'ecoder_auto_config_blocks_scripts') Configuration Wizard>
% blocks to obtain an initial configuration of a model for a specific goal.
% The predefined blocks provide configuration for:
% 
% * ERT optimized for fixed point
% * ERT optimized for floating point
% * GRT optimized for fixed and floating point
% * GRT debug settings for fixed and floating point
% * Custom (you provide the script)
% 
% Put the block into a model and double-click it to configure the model.
% Open model <matlab:rtwdemo_configwizard rtwdemo_configwizard> and
% click *Open Configuration Wizard Library* to interact with the blocks.

open_system(model)

%%
% To use configuration wizard blocks in the rtwdemo_configwizard model
% follow these steps:
%
% * Open the Configuration Wizard Library by clicking the link provided in the model.
% * Open the Model's Configuration Parameters by clicking the link provided in the model.
% * Drag and drop a Configuration Wizard Block, for example ERT (optimized for fixed point), from the wizard library into the model. 
% * Double-click the wizard block.
%
% The Configuration Parameter options are modified automatically.

% cleanup
rtwdemoclean;
close_system(model,0)

%% Summary
% Simulink provides a rich set of MATLAB functions to automate the
% configuring a model for simulation and code generation.
% Simulink Coder and Embedded Coder(R) provide additional functionality
% specific for code generation.  The Code Generation Advisor optimizes 
% the model configuration based on a set
% of prioritized goals. You can save the optimal configuration to
% a MATLAB file by using the configuration set saveAs function, 
% and reuse it across models and projects.

%   Copyright 2007-2015 The MathWorks, Inc.

