Create Data Objects for Code Generation with Data Object Wizard

To specify code generation options for signal lines, block parameters, and states in a model, you can use data objects that you store in a workspace or data dictionary. For basic information about data objects, see Data Objects (Simulink).

You can use the Data Object Wizard to create data objects for:

  • New or existing models that do not use data objects.

  • Existing models to which you have added signal lines or blocks.

This example shows how to use the Data Object Wizard to create and configure data objects for code generation from the built-in package Simulink.

Create Data Objects

Open the example model rtwdemo_basicsc.

open_system('rtwdemo_basicsc')

The model creates numeric variables in the base workspace. Blocks in the model use these variables to set parameter values (such as the Gain parameter of a Gain block). Some of the signals and block states in the model have explicit names, such as input1.

On the Modeling tab, under Design, click Data Object Wizard.

In the Data Object Wizard, click Find. The wizard proposes the creation of Simulink.Parameter objects to replace the variables and the creation of Simulink.Signal objects to represent the signals and states.

The wizard finds only signals, parameters, data stores, and states whose storage class is set to Auto. For example, if you use the Signal Properties dialog box to specify a storage class other than Auto for a signal line, the wizard does not propose a data object.

Click Select All.

Click Create. The data objects appear in the base workspace.

For detailed information about the options that you can choose in the Data Object Wizard, see Create Data Objects for a Model Using Data Object Wizard (Simulink).

Set Storage Class for Data Objects

Storage classes determine how the generated code uses variables to represent signals, parameters, and states. For data objects from the built-in package Simulink, the default storage class is Auto. To specify storage classes for the new data objects, use the Model Explorer.

Open the Model Explorer.

In the Model Hierarchy pane, select Base Workspace.

In the Contents pane, from the drop-down list Column View, select Storage Class.

Select all of the new data objects. For example, select the object input1, hold Shift, and select the object X.

Set the property StorageClass for all of the data objects to ExportToFile. To change the storage class for all of the selected objects, in the StorageClass column, click any of the objects. In the drop-down list, select ExportToFile. The change that you make propagates to all of the selected objects.

Specify the HeaderFile property for all of the objects as myExportedHdrFile.h.

In the model, set Configuration Parameters > Code Generation > System target file to ert.tlc. With this setting, the code generator honors storage classes such as ExportToFile.

Generate and Inspect Code

Generate code from the model.

### Starting build procedure for: rtwdemo_basicsc
### Successful completion of build procedure for: rtwdemo_basicsc

In the code generation report, view the generated file myExportedHdrFile.h. The file contains extern declarations for the global variables that correspond to the data objects.

/* Exported data declaration */

/* Declaration for custom storage class: ExportToFile */
extern int8_T K1;                      /* Referenced by: '<Root>/Gain' */
extern real_T K2;                  /* Referenced by: '<Root>/Stateflow Chart' */
extern real32_T LOWER;                 /* Referenced by: '<Root>/Constant2' */
extern real32_T T1Break[11];           /* Referenced by: '<Root>/Table1' */
extern real32_T T1Data[11];            /* Referenced by: '<Root>/Table1' */
extern real32_T T2Break[3];            /* Referenced by: '<Root>/Table2' */
extern real32_T T2Data[9];             /* Referenced by: '<Root>/Table2' */
extern real32_T UPPER;                 /* Referenced by: '<Root>/Constant1' */
extern real32_T X;                     /* '<Root>/Delay' */
extern real32_T input1;                /* '<Root>/In1' */
extern real32_T input2;                /* '<Root>/In2' */
extern real32_T input3;                /* '<Root>/In3' */
extern real32_T input4;                /* '<Root>/In4' */
extern boolean_T mode;                 /* '<Root>/Data Store Memory' */
extern real32_T output;                /* '<Root>/Stateflow Chart' */

View the file rtwdemo_basicsc.c. The file contains the definitions for the global variables. The code assigns numeric values for the variables that correspond to parameter objects.

/* Exported data definition */

/* Definition for custom storage class: ExportToFile */
int8_T K1 = 2;                         /* Referenced by: '<Root>/Gain' */
real_T K2 = 3.0;                   /* Referenced by: '<Root>/Stateflow Chart' */
real32_T LOWER = -10.0F;               /* Referenced by: '<Root>/Constant2' */
real32_T T1Break[11] = { -5.0F, -4.0F, -3.0F, -2.0F, -1.0F, 0.0F, 1.0F, 2.0F,
  3.0F, 4.0F, 5.0F } ;                 /* Referenced by: '<Root>/Table1' */

real32_T T1Data[11] = { -1.0F, -0.99F, -0.98F, -0.96F, -0.76F, 0.0F, 0.76F,
  0.96F, 0.98F, 0.99F, 1.0F } ;        /* Referenced by: '<Root>/Table1' */

real32_T T2Break[3] = { 1.0F, 2.0F, 3.0F } ;/* Referenced by: '<Root>/Table2' */

real32_T T2Data[9] = { 4.0F, 16.0F, 10.0F, 5.0F, 19.0F, 18.0F, 6.0F, 20.0F,
  23.0F } ;                            /* Referenced by: '<Root>/Table2' */

real32_T UPPER = 10.0F;                /* Referenced by: '<Root>/Constant1' */
real32_T X;                            /* '<Root>/Delay' */
real32_T input1;                       /* '<Root>/In1' */
real32_T input2;                       /* '<Root>/In2' */
real32_T input3;                       /* '<Root>/In3' */
real32_T input4;                       /* '<Root>/In4' */
boolean_T mode;                        /* '<Root>/Data Store Memory' */
real32_T output;                       /* '<Root>/Stateflow Chart' */

See Also

|

Related Topics