www.gusucode.com > ecoder 案例源码程序 matlab代码 > ecoder/GenerateCodeForAModelWithDimensionVariantsExample.m

    %% 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 use |Simulink.Parameter| objects to specify dimension information as
% symbols. For more information on signal dimensions, see <docid:simulink_ug.bst2z3h-1>.
%
% *Note:* The dimension variants feature is on by default. You can turn off
% this feature by clearing the <docid:simulink_gui.bu5jpqm-1>
% parameter on the *All Parameters* tab of 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.
% 
% <<../dimension_variants_model.png>>
%
%%
% # Open the model <matlab:rtwdemo_dimension_variants>.
% # Open the Model Explorer. Select the base workspace pane.
% # In the base workspace, there are four |Simulink.Parameter| objects for
% specifying symbolic dimensions. These |Simulink.Parameter| objects have the
% names |A|, |B|, |C|, and |D|.
% # Select the |Simulink.Parameter| object |A|. Review the information in the
% Simulink.Parameter dialog box. |A| has a storage class of |CompilerFlag|. 
% # Repeat Step 4 for each of the |Simulink.Parameter| objects |B|, |C|, and |D|.
% # 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.
%
% To use a Simulink.Parameter object for dimension specification, it must 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
%
% 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 <docid:simulink_ug.bt_5z2b-1>.
%% Specify Symbolic Dimensions for Blocks and Data Objects
% # Open the Source Block Parameters dialog box of Inport Block |In2|. In the
% *Signal Attributes* tab, the *Port Dimensions* field contains the
% |Simulink.Parameter| object |A|. For Inport blocks, you specify symbolic
% dimensions in the *Port Dimensions* field.
% # Open the Source Block Parameters dialog box of Inport block |In3|. In
% the *Signal Attributes* tab, the *Port Dimensions* field contains the
% |Simulink.Parameter| object |B|.
% # In the base workspace, select the |Simulink.Parameter| object |Data|.
% In the Simulink.Parameter dialog box for |Data|, 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, so the
% dimensions of |C| are consistent with the dimension of the |Data| object. The
% dimensions of the |Data| object must always be consistent with the value of
% the |Simulink.Parameter| object that is in the |Data| object *Dimensions*
% field.
% |Data| has a *Storage class* of |ImportedExtern|. A |Simulink.Parameter| object
% that uses a |Simulink.Parameter| for symbolic dimension specification must
% have a storage class of either |ImportedExtern| or |ImportedExternPointer|.
% # Open the Block Parameters dialog box of the |1-D Lookup Table1| block.
% The *Table data* field contains the |Simulink.Parameter|, |PT|. The
% *Breakpoints 1* field contains the |Simulink.Parameter|, |PB|.
% # In the base workspace, view the information in the Simulink.Parameter
% dialog boxes for |PB| and |PT|. These parameters contain the character vector
% |'[1,D]'| in their *Dimensions* field and are arrays consisting of 15 values.
% The dimension of |D| are 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.
%
% <<../dimension_variants_sim.png>>
%
% 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)