%% Apply Storage Classes to Data Items
% This example shows how to apply storage classes to a signal, a block
% state, and a block parameter in a model.
%% Explore Example Model
% Open the example model |rtwdemo_roll|.
open_system('rtwdemo_roll')
%%
% Navigate into the |BasicRollMode| subsystem.
open_system('rtwdemo_roll/BasicRollMode')
%%
% The subsystem contains an Integrator block, which maintains a piece of
% state data.
%
% From the root level of the model, navigate into the |RollAngleReference|
% subsystem.
open_system('rtwdemo_roll/RollAngleReference')
%%
% The subsystem contains a Constant block labeled UpThr.
%
% In this example, you configure the state and output signal of the
% Integrator block and the *Constant value* parameter of the Constant block
% to appear in the generated code as separate global variables.
%% Apply Storage Classes
% # Navigate to the root level of the model.
% # Open the *Simulink Coder* app. When prompted, change the system target
% file to |grt.tlc|.
% # Click the Model Data Editor tab.
% # In the Model Data Editor, click the *Change scope* button. This expands
% the lists of data elements to include in the subsystems.
% # Select the *Signals* tab.
% # Set the *Change view* drop-down list to |Code|.
% # In the *Filter contents* box, enter |integrator|.
% # Optionally, use the *Name* column to specify a name for the output signal
% of the Integrator block, such as |mySignal|.
% # Use the *Storage Class* column to apply storage class |ExportedGlobal|.
% # Select the *States* tab.
% # Optionally, use the *Name* column to specify a name for the state, such
% as |myState|.
% # Use the *Storage Class* column to apply storage class |ExportedGlobal|.
% # Select the *Parameters* tab. You cannot apply a storage class directly
% to a block parameter. You must create a parameter object, such as
% |Simulink.Parameter|, use the object to set the value of the block
% parameter, and apply the storage class to the object.
% # Set the *Change view* drop-down list to |Design|.
% # In the *Filter contents* box, enter |UpThr|.
% # Use the *Value* column to change the parameter value from |6| to
% |UpThr|.
% # While editing the parameter value, next to |UpThr|, click the action
% button (with three vertical dots) and select *Create*.
% # In the *Create New Data* dialog box, set *Value* to
% |Simulink.Parameter| and *Location* to |Base Workspace|.
% Click *Create*. A |Simulink.Parameter| object named |UpThr| appears in
% the base workspace.
% # In the |UpThr| property dialog box, set *Value* to |6| and *Storage
% class* to |ExportedGlobal|. Click *OK*.
%
% Alternatively, you can use these commands at the command prompt to
% configure the signal, state, and parameter data.

% Configure signal
portHandles = get_param('rtwdemo_roll/BasicRollMode/Integrator',...
'PortHandles');
outportHandle = portHandles.Outport;
set_param(outportHandle,'Name','mySignal')
set_param(outportHandle,'StorageClass','ExportedGlobal')

% Configure state
set_param('rtwdemo_roll/BasicRollMode/Integrator',...
'StateName','myState')
set_param('rtwdemo_roll/BasicRollMode/Integrator',...
'StateStorageClass','ExportedGlobal')

% Configure parameter
set_param('rtwdemo_roll/RollAngleReference/UpThr','Value','UpThr')
UpThr = Simulink.Parameter(6);
UpThr.StorageClass = 'ExportedGlobal';
%% Generate and Inspect Code
% Generate code from the model.
set_param('rtwdemo_roll','SystemTargetFile','grt.tlc')
rtwbuild('rtwdemo_roll')
%%
% Inspect the file |rtwdemo_roll.c|. The file defines the global variables.
file = fullfile('rtwdemo_roll_grt_rtw','rtwdemo_roll.c');
rtwdemodbtype(file,'/* Exported block signals */','real32_T myState;',1,1)
%%
% The file |rtwdemo_roll.h| declares the variables. To access these
% variables, your external code can include (|#include|) this header file.
%
% In |rtwdemo_roll.c|, search the file for the names of the variables, such
% as |UpThr|. The model algorithm in the |rtwdemo_roll_step| function reads
% and writes to the variables.

%%
% Copyright 2012 The MathWorks, Inc.