Flexible Storage Class for Different Model Hierarchy Contexts - MATLAB & Simulink (original) (raw)
Main Content
This example shows how to use one storage class throughout a model hierarchy to generate code that is unstructured for single-instance data and structured for multi-instance data. When a model hierarchy contains single-instance and multi-instance data, use a flexible storage class to specify the settings for the two contexts instead of creating two separate storage classes.
This example uses the single-instance top model ex_mdlreftop_dd
, which references the multi-instance model ex_mdlrefbot_dd
three times. Both models share the data dictionary ex_mdlref_dd.sldd
. When you define the storage class in the shared data dictionary, you can apply the class to data items in both models.
- Open the example model
ex_mdlreftop_dd
. - Open the shared data dictionary. On the Modeling tab, click Design > Data Dictionary.
- Open the shared Embedded Coder Dictionary. In the Model Explorer Model Hierarchy pane, expand the
ex_mdlref_dd
node and click Embedded Coder Dictionary. In the right pane, click Open Embedded Coder Dictionary. - On the left pane, click Storage Class.
- The built-in storage class
MultiInstance
is configured to generate a structure for multi-instance data. Duplicate this storage class so that you can edit other properties. Select theMultiInstance
storage class and click Duplicate. - In the Storage Classes section, click Create.
- For the new storage class, in the property inspector pane, set these property values:
- Name —
MyStorageClass
- Use different property settings for single-instance and multi-instance data — selected.
- Single-instance storage > Storage Type —
Unstructured
. - Multi-instance storage > Storage Type —
Structured
.
When you apply MyStorageClass
to a data item, the Embedded Coder Dictionary implements the single-instance settings or the multi-instance settings depending on the type of data and the context of the model within the model reference hierarchy.
- Review the implementations for the different settings in the pseudocode preview.
- Apply the storage class to internal data items by specifying it as a dictionary default. In the Embedded Coder Dictionary, click Data Defaults. For the Signals, states, and internal data row, set Storage Class to
MyStorageClass
. Click OK.Becauseex_mdlreftop_dd
andex_mdlrefbot_dd
share the dictionaryex_mdlref_dd.sldd
, both models useMyStorageClass
as the default storage class for internal data. - Open the Embedded Coder app for the model
ex_mdlreftop_dd
. - Generate code for the model.
- Review the generated code for the referenced model. To open the referenced model in the editor, double-click the Model block, CounterA. The reference model code appears in the Code view. In
ex_mdlrefbot_dd.h
, the reference model code defines the storage class structure in which it stores the internal data of the referenced model.
/* Storage class 'MyStorageClass', for model 'ex_mdlrefbot_dd' */
typedef struct {
real_T PreviousOutput_DSTATE; /* '<Root>/Previous Output' */
} ex_mdlrefbot_dd_MyStorageClass;
/* Real-time Model Data Structure */
struct ex_mdlrefbot_dd_tag_RTM {
const char_T **errorStatus;
ex_mdlrefbot_dd_MyStorageClass *MyStorageClass_ex_mdlrefbot_dd;
};
Because the referenced model is multi-instance, the definition implements the multi-instance data settings of MyStorageClass
. The reference model code stores the internal data in the structure ex_mdlrefbot_dd_MyStorageClass
.
- Navigate back to the top model and review the generated code. In
ex_mdlreftop_dd.c
, the top model code defines its internal data for each Model block by instantiating the storage class of the referenced model,ex_mdlrefbot_dd_MyStorageClass
.
/* Storage class 'MyStorageClass' */
ex_mdlrefbot_dd_MdlrefDW ex_mdlreftop_dd_CounterA_InstanceData;
ex_mdlrefbot_dd_MdlrefDW ex_mdlreftop_dd_CounterB_InstanceData;
ex_mdlrefbot_dd_MdlrefDW ex_mdlreftop_dd_CounterC_InstanceData;
/* Storage class 'MyStorageClass' */
ex_mdlrefbot_dd_MyStorageClass MyStorageClass_CounterA;
/* Storage class 'MyStorageClass' */
ex_mdlrefbot_dd_MyStorageClass MyStorageClass_CounterB;
/* Storage class 'MyStorageClass' */
ex_mdlrefbot_dd_MyStorageClass MyStorageClass_CounterC;
Because the top model is single-instance, these definitions implement the single-instance data settings of MyStorageClass
. The top model also packages its own internal data as standalone variables by using the single-instance data settings. The top model code does not contain a structure definition for internal data.