Main Content

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.

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

open_system('ConfigurationInterface')

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.

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: ConfigurationInterface
### Successful completion of code generation for: ConfigurationInterface

Build Summary

Top model targets:

Model                   Build Reason                                         Status           Build Duration
============================================================================================================
ConfigurationInterface  Information cache folder or artifacts were missing.  Code generated.  0h 0m 10.439s 

1 of 1 models built (0 models already up to date)
Build duration: 0h 0m 11.201s

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 real_T X;

#endif                                 /* myExportedHdrFile_h_ */

/*
 * File trailer for generated code.
 *
 * [EOF]
 */

View the file ConfigurationInterface.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 */
real_T X;

See Also

|

Related Topics