Choose Storage Class for Controlling Data Representation in Generated Code

A storage class is a code generation setting that you apply to model data, such as signals, block parameters, and states. Use storage classes to control the appearance of data elements in the generated code.

The table shows built-in storage classes that you can choose. These storage classes come with Simulink® Coder™.

Storage Class NameDescription
Auto

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 Configuration Parameters > Code Generation > Optimization pane, see How Generated Code Stores Internal Signal, State, and Parameter Data (Simulink Coder).

Optimizations cannot eliminate some data, such as most block states, from the code. This remaining data acquire a default storage class that you specify with the Code Mapping Editor (see Configure Default Code Generation for Data—requires Embedded Coder®). If you leave the storage class setting in the Code Mapping Editor at the default value, Default, the data element appears as a field of a standard data structure (see How Generated Code Stores Internal Signal, State, and Parameter Data).

If optimizations cannot eliminate the data element, the name of the element in the code is based on naming rules that you specify with model configuration parameters (see Identifier Format Control—requires Embedded Coder).

Use this storage class to enable optimizations to operate on the data element, potentially generating more efficient code.

Model default

The data element acquires the corresponding default storage class that you specify with the Code Mapping Editor. The name of the data element in the code is the same as the name in the model.

Use this storage class to prevent optimizations from eliminating storage for a data element (see How Generated Code Stores Internal Signal, State, and Parameter Data (Simulink Coder)).

ExportedGlobal

Generate a global variable definition and declaration. 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.

ImportedExtern

Generate code that reads from and writes to a global variable defined by your external code. The generated code declares the variable in the generated file model_private.h so that the model entry-point functions can read and write to the variable.

Use this storage class to make a data element in a model represent a global variable that your external code defines. The generated algorithmic code uses the variable without defining it.

ImportedExternPointer

Generate code that reads from and writes to a global pointer variable defined by your external code. The generated code declares the variable in the generated file model_private.h and reads and writes to the data by dereferencing the pointer.

Use this storage class when your external code defines a data element and provides a pointer for accessing that data.

If you have Embedded Coder, you can choose additional built-in storage classes, shown in the next table.

Storage Class NameDescriptionUse for Signal or State DataUse for Parameter Data

BitField

Generate a structure that stores Boolean data in named bit fields. For an example, see Bitfields.

You cannot use this storage class in the Code Mapping Editor.

Yes

Yes

CompilerFlag

Supports preprocessor conditionals defined via compiler flag or option. See Generate Preprocessor Conditionals for Variant Systems.

If you build the generated code by using Embedded Coder (you clear Configuration Parameters > Generate code only), to specify the compiler option, you can use the model configuration parameter Configuration Parameters > Code Generation > Custom Code > Additional build information > Defines. See Code Generation Pane: Custom Code: Additional Build Information: Defines (Simulink Coder).

No

Yes

Const

Generate a global variable definition and declaration with the const type qualifier.

No

Yes

ConstVolatile

Generate a global variable definition and declaration with the const and volatile type qualifiers. For an example, see Type Qualifiers.

No

Yes

Define

Generate a macro (#define directive) such as #define myParam 5. For an example, see Macro Definitions (#define).

No

Yes

ExportToFile

Generate a global variable definition and declaration. You can specify the names of the files that define and declare the variable.

Yes

Yes

FileScope

Generate a global variable definition and declaration with 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 as Simulink.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.

Yes

Yes

GetSetGenerate code that interacts with data by calling your custom accessor functions. Your external code defines the data and provides the function definitions. For examples, see Access Data Through Functions with Storage Class GetSet.

Yes

Yes

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

No

Yes

ImportFromFile

Generate code that reads from and writes to a global variable defined by your external code. Similar to ExportToFile, but the generated code does not define the variable.

Yes

Yes

Reusable

Generate more efficient code that stores intermediate calculations of a data path (a series of connected blocks) in a single, reused global variable. For an example, see Specify Buffer Reuse by Using Simulink.Signal Objects.

You can apply this storage class only to a Simulink.Signal object that represents multiple signal lines in a model.

Yes

No

Struct

Generate a global structure whose name you can specify. For examples, see Organize Parameter Data into a Structure by Using the Struct Storage Class and Structures of Signals.

You cannot use this storage class in the Code Mapping Editor.

Yes

Yes

Volatile

Generate a global variable definition and declaration with the volatile type qualifier.

Yes

Yes

LocalizableFor signals, if possible, generate variables that are local to functions rather than in global storage. Generating local variables prevents the code generator from implementing optimizations that remove the variables from the generated code. For an example, see Generate Local Variables with Localizable Storage Class.Yes (signals not states)No

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 with the Quick Start tool.

Storage Class NameDescriptionUse for Signal or State DataUse for Parameter Data
ParamStruct

Generate global structures that contain parameter data. In a hierarchy of components (referenced models or atomic subsystems), you can use this example storage class to create a corresponding hierarchy of parameter structures. This storage class appears only in the Code Mapping Editor for data category Model parameters after preparing a model for code generation with the Quick Start tool.

NoYes
SignalStruct

Generate global structures that contain signal or state data. In a hierarchy of components (referenced models or atomic subsystems), you can use this example storage class to create a corresponding hierarchy of signal and state structures. This storage class appears only in the Code Mapping Editor after preparing a model for code generation with the Quick Start tool.

YesNo

When you generate reentrant, multi-instance code, limitations and restrictions apply if you want to use storage classes. See Use Storage Classes in Reentrant, Multi-Instance Models and Components (Simulink Coder).

Specify File Names and Other Data Attributes With Storage Class (Embedded Coder)

After you apply some storage classes, such as ExportToFile, you can specify additional settings such as declaration (header) and definition file names. In the Code Mapping Editor, to access these additional settings, you use the Property Inspector.

With other storage classes, you cannot specify additional settings.

  • These built-in Simulink Coder storage classes do not allow you to specify additional settings:

    • ExportedGlobal

    • ImportedExtern

    • ImportedExternPointer

  • The built-in Embedded Coder storage classes typically allow you to specify declaration and definition file names.

  • For the ExportToFile storage class, the definition file setting is honored only for single-instance models. The setting is ignored for reusable multi-instance models because the data definition is handled at the parent level.

  • When you create a storage class with 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.

Specify Default #include Syntax for Header Files That Declare Data (Embedded Coder)

To control the file placement of a data item, such as a signal line or block state, in the generated code, you can apply a storage class to the data item (see Apply Built-In and Customized Storage Classes to Data Elements). You then use the Header file custom attribute to specify the generated or external header file that contains the declaration of the data.

To reduce maintenance effort and data entry, when you specify Header file, you can omit delimiters (" or <>) and use only the file name. You can then control the default delimiters that the generated code uses for the corresponding #include directives. To use angle brackets by default, set Configuration Parameters > Code Generation > Code Placement > #include file delimiters to #include <header.h>.

Storage Class Limitations

  • When you use storage classes in the Code Mappings editor (Embedded Coder), some limitations apply. See Constraints.

  • Data objects cannot use an Embedded Coder storage class and a multiword data type.

  • For Embedded Coder storage classes in models that use referenced models:

    • If you apply a grouped storage class to multiple data items, the storage class Data scope property must be set to Imported and you must provide the data declaration in an external header file. Grouped storage classes use a single variable in the generated code to represent multiple data objects. For example, the storage classes BitField and Struct are grouped storage classes.

    • If a parameter object exists in the base workspace or a data dictionary, and a referenced model uses the object, you cannot apply the storage class FileScope. As a workaround, move the object into the model workspace of the referenced model. Then, you can use FileScope.

  • You cannot apply the storage class FileScope to data items used by a data exchange interface (C API, external mode, or ASAP2) or MAT-file logging. File-scoped data is not externally accessible.

Related Topics