Main Content

Flexible Storage Class for Different Model Hierarchy Contexts

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.

  1. Open the example model ex_mdlreftop_dd.

  2. Open the shared data dictionary. On the Modeling tab, click Design > Data Dictionary.

  3. 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.

  4. On the left pane, click Storage Class.

  5. 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 the MultiInstance storage class and click Duplicate.

  6. In the Storage Classes section, click Create.

  7. For the new storage class, in the property inspector pane, set these property values:

  • NameMyStorageClass

  • Use different property settings for single-instance and multi-instance data — selected.

  • Single-instance storage > Storage TypeUnstructured.

  • Multi-instance storage > Storage TypeStructured.

coderdict_single_multi_SC.png

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.

  1. Review the implementations for the different settings in the pseudocode preview.

  2. 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.Because ex_mdlreftop_dd and ex_mdlrefbot_dd share the dictionary ex_mdlref_dd.sldd, both models use MyStorageClass as the default storage class for internal data.

  3. Open the Embedded Coder app for the model ex_mdlreftop_dd.

  4. Generate code for the model.

  5. 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.

  1. 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.

See Also

Related Topics