MATLAB Examples

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 docid:simulink_ug.f14-90636.

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.

Contents

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.

In the model, select Code > Data Objects > 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 docid:simulink_ug.bqgy1ty.

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, you can 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 custom storage classes such as ExportToFile.

Generate and Inspect Code

Generate code from the model.

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

In the code generaetion 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;
extern real_T K2;
extern real32_T LOWER;
extern real32_T T1Break[11];
extern real32_T T1Data[11];
extern real32_T T2Break[3];
extern real32_T T2Data[9];
extern real32_T UPPER;
extern real32_T X;
extern real32_T input1;
extern real32_T input2;
extern real32_T input3;
extern real32_T input4;
extern boolean_T mode;
extern real32_T output;

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;
real_T K2 = 3.0;
real32_T LOWER = -10.0F;
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 } ;

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 } ;

real32_T T2Break[3] = { 1.0F, 2.0F, 3.0F } ;

real32_T T2Data[9] = { 4.0F, 16.0F, 10.0F, 5.0F, 19.0F, 18.0F, 6.0F, 20.0F,
  23.0F } ;

real32_T UPPER = 10.0F;
real32_T X;
real32_T input1;
real32_T input2;
real32_T input3;
real32_T input4;
boolean_T mode;
real32_T output;