Choose Storage Class for Controlling Data Representation in Generated Code - MATLAB & Simulink (original) (raw)

A storage class is a code generation setting that you apply to data, such as parameters, signals, and states. During code configuration, use a storage class to control the appearance and placement of a data element in the generated code and to prevent optimizations from eliminating storage for that data element.

For model data, you can apply a storage class directly to a model data element by using the Code Mappings editor or code mappings API. Using either the editor or the API, apply a default storage class for categories of data, and then override that setting, as needed, for individual data elements.

For external data, you apply a storage class to a data object by using the Model Explorer or programmatically by using functions such as get_param andset_param.

Once you have specified a storage class for an element or category of elements, you can set properties for that storage class, such as header files, definition files, and memory sections.

Storage Class Properties

The property settings for a storage class define how and where the code generator represents data in the generated code. The properties that the user of a storage class can configure vary depending on the storage class. For most individual elements, the user can configure an identifier for naming the data element in the code. To identify properties that the user can configure for built-in and predefined storage classes, see the following information on specific storage classes. For a comparison of available properties for each built-in or predefined storage class, see Choose Built-In or Predefined Storage Class Based on Properties.

When you create a storage class by using the Custom Storage Class Designer, you can choose whether the user of the storage class can specify additional settings. See Allow Users of Storage Class to Specify Property Value.

Default Storage Class

By default, the storage class for an individual data element is Auto. By using this default value, you are choosing to have the code generator handle storage class selection and application. In this case, the data element is subject to code generation optimizations, which can eliminate the element from the code or change the representation of the element. If optimizations do not eliminate the data element, the element appears as a field of a standard data structure in the generated code.

When using the Code Mappings editor or the code mappings API, a data element configured with storage class Auto that is not eliminated by optimizations acquires the default code generation settings for the corresponding data category. If you do not specify a code configuration for a category of data elements, the storage class isDefault. When a data element category is using this storage class, data elements in that category appear as a field of a standard data structure in the generated code.

Built-in and Predefined Storage Classes

To prevent optimizations from eliminating storage for a data element, you can choose a storage class for the element based on your code generation requirements. Available storage classes include built-in storage classes, predefined storage classes in theSimulink package, and might also include other project-specific storage classes defined in an Embedded Coder Dictionary. If you have special requirements that are not met by the listed storage classes and you are generating code for an ERT-based target, you can define and use a new storage class. See Define Service Interfaces, Storage Classes, Memory Sections, and Function Templates for Software Architecture and Create Storage Classes by Using the Custom Storage Class Designer.

For Simulink® Coder™, you can choose from these built-in and predefined storage classes.

Requirements Storage Class
For data elements that cannot be optimized away, represent data as a field of a standard data structure. Default (Default mapping)
Enable optimizations, potentially generating more efficient code. Use the default storage class for the data element category. Auto (Individual data elements)
Prevent optimizations from eliminating storage for a data element and use the default storage class for the data element category. Model Default (Individual mapping)
Generate standalone global variables. Generated code contains the variable declaration and definition. ExportedGlobal
Generate standalone global variables or global variable pointers. Generated code contains the variable or pointer declaration. Your external code provides the definition. ImportedExtern, ImportedExternPointer

If you have Embedded Coder®, you can choose from these additional predefined storage classes available in the Simulink package.

Requirements Storage Class
Prevent optimizations from eliminating storage for a data element. Dictionary Default (Default mapping)
Generate standalone global variables with const,volatile, or const andvolatile qualifiers. Const, Volatile, and ConstVolatile
Generate a macro (#define directive) or code that uses a macro defined in a header file in your external code. Define, ImportedDefine
Generate standalone global variables. You can specify the external files that contain the variable declaration and definition. ExportToFile
Generate standalone global variables. You can specify the external file that contains the variable declaration. Your external code provides the definition. ImportFromFile
Generate standalone variables with file scope. FileScope
Generate standalone variables whose scope is defined by their usage. Attempt to minimize the use of global scope by using variables localized to a function or file where possible. Localizable
Generate variables as fields of a structure. Struct
Generate variables as fields of a structure that stores Boolean, fixed-point, or integer data in named bitfields. Bitfield
Generate variables that you access through a function call. GetSet
Support preprocessor conditionals defined by using a compiler flag or option. CompilerFlag
Generate standalone global variables that enable buffer reuse. Reusable
Generate variables for single-instance data and generate structures for multi-instance data. MultiInstance

These storage classes are examples of storage classes that you can add to an Embedded Coder Dictionary. These examples are defined and made available when you prepare a model for code generation by using the Quick Start tool.

Requirements Storage Class
Generate global structures that contain parameter data and signal or state data. ParamStruct, SignalStruct

Note

Signal objects defined in the base workspace or a data dictionary with a non-auto storage class are not supported for models that are configured with an ERT-based system target file and service code interface.

Default

On the Data Defaults tab of the Code Mappings editor,Default is the default storage class setting for each data element category. If you leave the storage class setting for a category at this value, data elements that are not subject to code generation optimizations appear as a field of a standard data structure. See How Generated Code Stores Internal Signal, State, and Parameter Data.

Auto

When you select this storage class, the code generator handles how to represent the data in the generated code and can enable optimizations to operate on the data element, potentially generating more efficient code.

Auto is the default storage class setting for each data element in a model. The data element is subject to code generation optimizations, which can eliminate the element from the code or change the representation of the element. For information about these optimizations, such as those on the > > pane, see How Generated Code Stores Internal Signal, State, and Parameter Data.

Optimizations cannot eliminate some data, such as most block states, from the code. The remaining data acquires a default storage class that you specify on the Data Defaults tab of the Code Mappings editor (see Configure Default Code Generation for Data). If a data element cannot be eliminated, the name of the element in the code is based on naming rules that you specify by using model configuration parameters. See Identifier Format Control—requires Embedded Coder.

Model Default

When you configure an individual data element for code generation, use theModel Default storage class to prevent optimizations from eliminating storage for a data element. See How Generated Code Stores Internal Signal, State, and Parameter Data. With this storage class setting, the data element acquires the default storage class that you specify for the corresponding data category on the Data Defaults tab of the Code Mappings editor. The name of the data element in the code is the same as the name in the model.

Dictionary Default

In Embedded Coder, if you link a model to a data dictionary, which includes a coder dictionary that configures default code definitions for categories of data, you can use the Code Mappings editor to apply the dictionary defaults. On the Data Defaults tab, select a category, then set the storage class to Dictionary Default. For more information on configuring a default code mapping in a coder dictionary, see Configure Default Code Mapping in a Shared Dictionary.

ExportedGlobal

Use this built-in storage class to generate a global variable definition and declaration. By default, the name of the variable is the name of the data element. The code declares the variable in the generated file_`model`_.h, which you can include (#include) in your external code. The code generator does not optimize this global variable from the generated code.

When you use this storage class, you can also configure this property.

Property Description
Identifier Identifier string that the code generator uses to name the data element in the generated code.

For an example that uses this storage class, see C Data Code Interface Configuration for Model Interface Elements.

ImportedExtern, ImportedExternPointer

To make a data element in a model represent a global variable that your external code defines, use the built-in storage class ImportedExtern. The generated algorithmic code uses the variable without defining it.

Generate code that reads from and writes to a global variable defined by your external code. The generated code declares the variable in a header file so that the model entry-point functions can read and write to the variable. The model configuration parameterFile packaging format determines the header file that contains the declarations. See Imported Scope Data Declaration File Packaging.

For example, you can apply the storage class ImportedExtern to a signal line, block state, or parameter object. For imported data:

When your external code defines a data element and provides a pointer for accessing that data, use ImportedExternPointer. The generated code reads from and writes to that pointer. The generated code declares the variable in the header file determined byFile packaging format, and reads and writes to the data by dereferencing the pointer.

When you use these storage classes, you can also configure this property.

Property Description
Identifier Identifier string that the code generator uses to name the data element in the generated code.

Const, Volatile, and ConstVolatile

Use these storage classes to generate a global variable definition and declaration with the const, volatile, or const andvolatile type qualifiers.

When you use these storage classes, you can also configure these properties.

Property Description
Identifier Identifier string that the code generator uses to name the data element in the generated code.
HeaderFile Source header file that contains declarations for global data read by the data element and external code.If you do not specify a header file, the model configuration parameter Data declaration determines where the declarations are placed. SeeExported Scope Data Declaration File Packaging.
DefinitionFile Source definition file that contains definitions for global data read by the data element and external code.If you do not specify a definition file, the model configuration parameter Data definition determines where the definitions are placed. SeeData Definition File Packaging.
Owner A component in the model hierarchy where the code generator places a global data definition instead of placing it in the top component of the hierarchy. To use this property, you must set the model configuration parameterUse owner from data object for data definition placementIf you specify an owner other than the current model, no source definition file is generated for the current model. SeeData Definition File Packaging
PreserveDimensions Flag that indicates whether to declare multidimensional data as multidimensional arrays in the generated code. To use this flag, you must set the model configuration parameter Array layout to Row-major.

For an example that uses this storage class, see Type Qualifiers.

Define, ImportedDefine

Use the Define storage class to generate a macro (#define directive) for parameters such as #define myParam 5.

Use ImportedDefine to generate code that uses a macro (#define directive) defined in a header file in your external code. For an example, see Macro Definitions (#define).

When you use these storage classes, you can also configure these properties.

Property Description
Identifier Identifier string that the code generator uses to name the data element in the generated code.
HeaderFile Source header file that contains declarations for global data that is read by the data element and external code.ForDefine, if you do not specify a header file, the model configuration parameter Data declaration determines where the macro definitions are placed. See Exported Scope Data Declaration File Packaging.ForImportedDefine, this property must be specified.

Data elements that use this storage class do not appear in the code descriptor API or generated ARXML descriptions. For information about the code descriptor API, see Get Code Description of Generated Code. For information about generating ARXML descriptions, see Generate AUTOSAR C Code and XML Descriptions (AUTOSAR Blockset).

ExportToFile

Generate a global variable definition and declaration to an external file. You can specify the name and placement of the files that define and declare the variable. See Control Placement of Global Data Definitions and Declarations in Generated Files.

When you use this storage class, you can also configure these properties.

Property Description
Identifier Identifier string that the code generator uses to name the data element in the generated code.
HeaderFile Source header file where the code generator places the declarations for global data read by the data element and external code.If you do not specify a header file, the model configuration parameter Data declaration determines where the declarations are placed. SeeExported Scope Data Declaration File Packaging.
DefinitionFile Source definition file where the code generator places the definitions for global data read by the data element and external code.The code generator honors this setting only for single-instance models. The setting is ignored for reusable multi-instance models because the data definition is handled at the parent level.If you do not specify a definition file, the model configuration parameter Data definition determines where the definitions are placed. SeeData Definition File Packaging.
Owner A component in the model hierarchy where the code generator places a global data definition instead of placing it in the top component of the hierarchy. To use this property, you must set the model configuration parameterUse owner from data object for data definition placementIf you specify an owner other than the current model, no source definition file is generated for the current model. SeeData Definition File Packaging
PreserveDimensions Flag that indicates whether to declare multidimensional data as multidimensional arrays in the generated code. To use this flag, you must set the model configuration parameter Array layout to Row-major.

For an example that uses this storage class, see Definition, Initialization, and Declaration of Parameter Data.

ImportFromFile

Use this storage class to generate code that reads from and writes to a global variable defined by your external code. ImportFromFile is similar toExportToFile, but the generated code does not define the variable.

When you use this storage class, you can also configure these properties.

Property Description
Identifier Identifier string that the code generator uses to name the data element in the generated code.
HeaderFile Source header file that contains declarations for global data that is read by the data element and external code. To avoid compiler errors, you should add include guards to the beginning of the header file.If you do not specify a header file, the model configuration parameter File packaging format determines the header file that contains the declarations. See Imported Scope Data Declaration File Packaging.
PreserveDimensions Flag that indicates whether to declare multidimensional data as multidimensional arrays in the generated code. To use this flag, you must set the model configuration parameter Array layout to Row-major.

For an example that uses this storage class, see Integrate External Application Code with Code Generated from PID Controller.

FileScope

Use this storage class to generate a global variable definition and declaration that has the static type qualifier. In the generated code, the scope of the variable is limited to the current file, which is typically_`model`_.c.

In a model reference hierarchy, if a referenced model uses a parameter object (such asSimulink.Parameter) that you create in the base workspace or a data dictionary, you cannot apply FileScope to the object. As a workaround, move the parameter object into the model workspace of the referenced model. Then, you can use FileScope.

When you use this storage class, you can also configure these properties.

Property Description
Identifier Identifier string that the code generator uses to name the data element in the generated code.
PreserveDimensions Flag that indicates whether to declare multidimensional data as multidimensional arrays in the generated code. To use this flag, you must set the model configuration parameter Array layout to Row-major.

Localizable

For signals and states, try to minimize the use of global scope by declaring variables of file or function scope.

Generating a local variable of function scope prevents the code generator from optimizing the variable from the generated code (as would occur if theAuto storage class had been used).

For generated global variables, the model configuration parameters Data declaration and Data definition determine, respectively, where declarations and definitions are placed. See Exported Scope Data Declaration File Packaging and Data Definition File Packaging.

When you use this storage class, you can also configure these properties.

Property Description
Identifier Identifier string that the code generator uses to name the data element in the generated code.
PreserveDimensions Flag that indicates whether to declare multidimensional data as multidimensional arrays in the generated code. To use this flag, you must set the model configuration parameter Array layout to Row-major.

For an example that uses this storage class, see Generate Local Variables with Localizable Storage Class.

Struct

Use this storage class to generate a global structure with a name that you can specify.

When you use this storage class, you can also configure these properties.

Property Description
Identifier Identifier string that the code generator uses to name the data element in the generated code.
StructName Name of the structure for the data element in the generated code.

For an example that uses this storage class, see Organize Parameter Data into a Structure by Using Struct Storage Class and Structures of Signals.

Bitfield

Use this storage class to generate a structure that stores Boolean, fixed-point, or integer data in named bit fields.

When you use this storage class, you can also configure these properties.

Property Description
Identifier Identifier string that the code generator uses to name the data element in the generated code.
StructName Name of the structure for the data element in the generated code.

For an example that uses this storage class, see Bitfields.

GetSet

Use this storage class to generate code that interacts with data by calling custom accessor functions. Your external code defines the data and provides the function definitions.

When you use this storage class, you can also configure these properties.

Property Description
Identifier Identifier string that the code generator uses to name the data element in the generated code.
HeaderFile Source header file that contains declarations for theget and set functions.
GetFunction Data element that appears in the generated code as a call to a specifiedget function.
SetFunction Data element that appears in the generated code as a call to a specifiedset function.
PreserveDimensions Flag that indicates whether to declare multidimensional data as multidimensional arrays in the generated code. To use this flag, you must set the model configuration parameter Array layout to Row-major.

For an example that uses this storage class, see Access Data Through Functions with Storage Class GetSet.

CompilerFlag

Use this storage class to support preprocessor conditionals defined by using a compiler flag.

To specify the flag when building the code using Embedded Coder, you can use the model configuration parameter > > > > . See Code Generation Pane: Custom Code: Additional Build Information: Defines.

When you use this storage class, you can also configure this property.

Property Description
Identifier Identifier string that the code generator uses to name the data element in the generated code.

For an example that uses this storage class, see Compile Code Conditionally for Variations of Component Represented Using Variant Block.

Reusable

Use this storage class to reuse the same variable for multiple independent signals in a model. The code generator stores intermediate calculations of a data path (a series of connected blocks) in a single, reused global variable.

When you use this storage class, you can also configure these properties.

Property Description
Identifier Identifier string that the code generator uses to name the data element in the generated code.
DataScope Specification of the scope of the generated data. Choose from the following settings: Auto — Let the code generator determine the data scope.File — Generate data with file scope.Exported — Generate global data. Generated code declares and defines the data.Imported — Generate global data. Generated code declares the data and imports the data definition from external code.
HeaderFile Source header file where the code generator places the declarations for global data read by the data element and external code.If the data scope is Exported, and you do not specify a header file, the model configuration parameter Data declaration determines where the declarations are placed. See Exported Scope Data Declaration File Packaging.If the data scope is Imported, and you do not specify a header file, the model configuration parameter File packaging format determines the header file that contains the declarations. See Imported Scope Data Declaration File Packaging.If the data scope is File, this setting has no effect.
DefinitionFile Source definition file where the code generator places the definitions for global data read by the data element and external code.The code generator honors this setting only for single-instance models. The setting is ignored for reusable multi-instance models because the data definition is handled at the parent level.If the data scope isExported, and you do not specify a definition file, the model configuration parameter Data definition determines where the definitions are placed. See Data Definition File Packaging.If the data scope is File orImported, this setting has no effect.
Owner A component in the model hierarchy where the code generator places a global data definition instead of placing it in the top component of the hierarchy. To use this property, you must set the model configuration parameterUse owner from data object for data definition placementIf the data scope isExported, and you specify an owner other than the current model, no source definition file is generated for the current model. See Data Definition File Packaging.If the data scope is File orImported, this setting has no effect.

For an example that uses this storage class, see Specify Buffer Reuse for Signals in a Path.

MultiInstance

To generate unstructured variables for single-instance data and structures for multi-instance data, use this storage class. When you apply this storage class to a data item, the Embedded Coder Dictionary determines if it is a single-instance storage class or a multi-instance storage class. The dictionary determines the type of class by the type of data and by the context of the model within the model reference hierarchy. You can duplicate this storage class to create a copy that you can edit.

When you apply this storage class to an individual data element, you can also configure the Identifier property. The Identifier property is an identifier string that the code generator uses to name the data element in the generated code.

For an example that uses this storage class, see Flexible Storage Class for Different Model Hierarchy Contexts.

ParamStruct, SignalStruct

Use these storage classes to generate global structures that contain parameter and signal or state data, respectively. In a hierarchy of components (referenced models or atomic subsystems), you can use these storage classes to create a corresponding hierarchy of structures. These storage classes appear in the Code Mappings editor only after preparing a model for code generation by using the Quick Start tool.

These storage classes are available only for model-owned data.

When you use these storage classes, you can also configure this property.

Property Description
Identifier Identifier string that the code generator uses to name the data element in the generated code.

Storage Class Limitations

See Also

Topics