%% Switch Between Output Waveforms During Code Execution for Waveform Generator Block
% This example shows how to generate code that enables you to switch
% between stimulus waveforms during code execution. 
%
% For a Waveform Generator block, you cannot make the parameters of a
% waveform, such as amplitude and phase shift, tunable in the generated
% code. Instead, you can generate code that enables you to choose an active
% waveform from a set of waveform variants that you specify in the block.
% During execution of the code, you activate a variant by 
% adjusting the value of a global
% structure field. 
%
% You must set the model configuration parameter *Default parameter
% behavior* to |Tunable|. Then, by default, block parameters in
% the model appear tunable in the generated code. These parameters can
% consume large amounts of memory for a large model.
%
% Create the model |ex_switch_waveform|.
open_system('ex_switch_waveform')
%%
% In the Waveform Generator block, configure this waveform:
%
%   square(amp,10,0,dutyCycle)
%
% In the base workspace, create the variables |amp| and |dutyCycle|.
amp = 2.71;
dutyCycle = 50;
%%
% This waveform represents the baseline stimulus that you want the
% application to use at the start of execution.
%
% Suppose that, during execution, you want to observe the
% effects of changing the waveform frequency from 10 to 15 and the phase
% shift
% from 0 to 0.5. In the Waveform Generator block, add these waveform variants:
%
%   square(amp,10,0.5,dutyCycle)
%   square(amp,15,0,dutyCycle)
%   square(amp,15,0.5,dutyCycle)  
%
% Set the model configuration parameter *Default parameter behavior* to
% |Tunable|.
set_param('ex_switch_waveform','DefaultParameterBehavior','Tunable')
%%
% Generate code from the model.
rtwbuild('ex_switch_waveform')
%%
% The generated file |ex_switch_waveform.h| defines the standard structure type that
% stores tunable parameter data for the model. The structure contains a field whose value
% represents the active waveform.
file = fullfile('ex_switch_waveform_grt_rtw','ex_switch_waveform.h');
rtwdemodbtype(file,'/* Parameters (default storage) */',...
    '/* Real-time Model Data Structure */',1,0)
%%
% The file |ex_switch_waveform_data.c| defines a global structure variable
% and initializes the field value to |1|. This value represents the baseline waveform.
file = fullfile('ex_switch_waveform_grt_rtw','ex_switch_waveform_data.c');
rtwdemodbtype(file,'/* Block parameters (default storage) */','};',1,1)
%%
% The file |ex_switch_waveform.c| defines the model execution function. The function
% uses a |switch| statement to determine the value of the active
% waveform, and then calculates the value of the root-level Outport block, |Out1|.
file = fullfile('ex_switch_waveform_grt_rtw','ex_switch_waveform.c');
rtwdemodbtype(file,'switch ((int32_T)',...
    'ex_switch_waveform_Y.Out1 =',1,1)
%%
% During code execution, to change the active waveform, adjust the value of
% the structure field in the global parameters structure.

%% 
% Copyright 2012 The MathWorks, Inc.