%% Dimension Variants
% Use symbolic dimensions to simulate various sets of dimension choices without regenerating code for every set. Set up your model with dimensions that you specify as symbols in blocks and data objects. These symbols propagate throughout the model during simulation, and then go into the generated code. Modeling constraints for symbols during simulation (for example, C=A+B) are output as preprocessor conditionals in either the model.h or the model _types.h file.
% You can directly specify dimension information as a symbol or a numeric constant for these blocks and data objects:
% Inport
% Outport
% Signal Specification
% Data Store Memory
% Interpreted MATLAB Function
% Simulink.Signal
% Simulink.Parameter
% Simulink.BusElement
% AUTOSAR.Parameter
% The Data Store Memory and Interpreted MATLAB Function blocks also support variable dimension signals. For these blocks, the symbolic dimensions control the maximum allowed size.
% You must provide the initialization code for Simulink.Parameter objects that contain symbolic dimensions. To prevent the generated code from initializing these parameters, you must either:
% Configure the parameters to use a storage class with the Data scope property set to Imported, such as the ImportedExtern or ImportedExternPointer built-in storage classes.
% Configure the parameters to use a storage class with the Data initialization property set to None.
% You use Simulink.Parameter objects to specify dimension information as symbols. For more information on signal dimensions, see Signal Dimensions.
% You must fix symbolic dimension values at compile-time. You can not resolve a symbolic dimension to another variable and then vary it during run-time because you define the behavior during simulation.
% Note: The dimension variants feature is on by default. You can turn off this feature by clearing the Allow symbolic dimension specification parameter in the Configuration Parameters dialog box.
%% Define Symbolic Dimensions
% This example uses the model rtwdemo_dimension_variants to show how to implement symbolic dimensions. This model has four modeling patterns involving vectors and matrices.
%
% To show block names, on the Debug tab, on the Information Overlays menu, clear the Hide Automatic Block Names parameter.
% Open the Embedded Coder app. In the C Code tab, select Code Interface > Individual Element Code Mappings.
% In the Code Mappings editor, on the Parameters tab, click the refresh button. Seven Simulink.Parameter objects appear. Four of these objects are for specifying symbolic dimensions. These Simulink.Parameter objects have the names A, B, C, and D. Notice that these parameters have a storage class of CompilerFlag.
% To use a Simulink.Parameter object for dimension specification, it must be defined in the base workspace and have one of these storage classes:
% Define or ImportedDefine with header file specified
% CompilerFlag
% User-defined custom storage class that defines data as a macro in a specified header file
% For Simulink.Parameter objects with an ImportedDefine custom storage class, provide a header file on the MATLAB path. Insert the name of the header file in the HeaderFile field in the Simulink.Parameter dialog box.
% You can use MATLAB expressions to specify symbolic dimensions. For a list of supported MATLAB expressions, see the section Operators and Operands in Variant Condition Expressions in Introduction to Variant Controls.
%% Specify Symbolic Dimensions for Blocks and Data Objects
% Click Inport Block In2. On the Property Inspector, on the Parameters tab, the Port Dimensions field contains the Simulink.Parameter object A. For Inport blocks, you specify symbolic dimensions in the Port Dimensions field.
% Click Inport block In3. The Port Dimensions field contains the Simulink.Parameter object B.
% Click the Constant block. The Constant value parameter has a value of Data. In the Code Mappings editor, on the Parameters tab, click the Simulink.Parameter object Data. On the Property Inspector, the Dimension field has the character vector '[1,C]' , which is equivalent to '[1,5]' because C has a value of 5. The Value field contains an array with 5 values, which is consistent with its dimensions. The dimensions of the data object must be consistent with the value of the Simulink.Parameter object that is in the Dimensions field. Notice that Data has a Storage class of ImportedExtern.
% Open the 1-D Lookup Table1 block parameters dialog box. The Table data field contains the Simulink.Parameter, PT. The Breakpoints 1 field contains the Simulink.Parameter, PB.
%  In the Code Mappings editor, click PB and PT and view their properties. These parameters contain the character vector '[1,D]' in their Dimensions field and are arrays consisting of 15 values. The value of D is consistent with the dimension of the PB and PT parameters because D has a value of 15 .
% Simulate the model. Simulink propagates the dimensions symbolically in the diagram. During propagation, Simulink establishes modeling constraints among symbols. Simulink then checks for consistency with these constraints based on current numerical assignments. One modeling constraint for rtwdemo_dimension_variants is that C=A+B. The Diagnostic Viewer produces a warning for any violations of constraints.
% Change the dimension specification to a different configuration and simulate the model again.
%
% Though not shown in this example, you can specify an n-D dimension expression with one or more of the dimensions being a symbol (for example, '[A,B,C]' or '[1,A,3]').
%% Generate Code for a Model with Dimension Variants
% Once you have verified dimension specifications through model simulation, generate code for rtwdemo_dimension_variants.
% Create a temporary folder for the build and inspection process.

currentDir = pwd;
[~,cgDir] = rtwdemodir();

%
% Build the model.

model='rtwdemo_dimension_variants';
rtwbuild(model)

%
% View the generated code. In the rtwdemo_dimension_variants.h file, symbolic dimensions are in data declarations.

hfile = fullfile(cgDir,'rtwdemo_dimension_variants_ert_rtw',...
    'rtwdemo_dimension_variants.h');
rtwdemodbtype(hfile,'/* External inputs', '/* Real-time', 1, 0);

%
% The rtwdemo_dimension_variants.h file contains data definitions and preprocessor conditionals that define constraints established among the symbols during simulation. One of these constraints is that the value of a symbolic dimension must be greater than 1. This file also includes the user-provided header file for any Simulink.Parameter objects with an ImportedDefine custom storage class.

hfile = fullfile(cgDir,'rtwdemo_dimension_variants_ert_rtw',...
    'rtwdemo_dimension_variants.h');
rtwdemodbtype(hfile,'#ifndef A', '/* Macros for accessing', 1, 0);

%
% In the rtwdemo_dimension_variants.c file, symbolic dimensions participate in loop bound calculations, array size and index offset calculations, and a parameterized utility function (for example, Lookup Table block) calculation.

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

%
% Close the model and code generation report.

bdclose(model)
rtwdemoclean;
cd(currentDir)

% Copyright 2012 The MathWorks, Inc.