Apply Built-In and Customized Storage Classes to Data Elements

In Apply Storage Classes to Individual Signal, State, and Parameter Data Elements, you control the declaration and definition of global variables in the generated code by applying storage classes directly to individual data elements. With Embedded Coder® and an ERT-based system target file, you can have more precise control over the way that data appear in the generated code.

To achieve C code patterns such as grouping variables into flat structures and to control declaration and definition file placement, you can use the storage classes, available with Embedded Coder, from the built-in package Simulink.

For information about applying storage classes to categories of data by default, by using the Code Mapping Editor, see Configure Default Code Generation for Data.

Built-In Storage Classes

For a list of built-in storage classes that you can choose, see Choose Storage Class for Controlling Data Representation in Generated Code.

Create Your Own Storage Class

If the storage classes from the Simulink package do not satisfy your requirements, you can define your own storage classes. For information about defining your own storage class, see Create Code Definitions to Override Default Settings.

Techniques to Apply Storage Classes Interactively

To directly apply a storage class from the built-in package Simulink interactively, use the techniques described in Apply Storage Classes to Individual Signal, State, and Parameter Data Elements.

To apply a storage class from a package other than Simulink (such as a package that you create):

Target Class Does Not Appear in List of Signal Object Classes

When you use the Signal object class drop-down list in a dialog box or in the Property Inspector, if the class that you want does not appear in the list:

  1. From the list, select Customize class lists.

  2. In the dialog box, under Signal classes, select the check box next to the class that you want. For example, to use storage classes from the built-in package mpt, select the check box next to mpt.Signal. Click OK.

    If you created your own package, the classes that the package defines appear in the dialog box only if you put the package folder in your current folder or put the parent folder of the package folder on the MATLAB® path.

  3. From the drop-down list, select the option that corresponds to what you selected. For example, select mpt.Signal.

Apply Storage Class from Specific Package to Signal, State, or Data Store Memory Block Using the Model Data Editor

When you use the Model Data Editor, to apply storage classes from a specific package, use the Model Explorer to create a signal object from the target package. Then, when you open the Model Data Editor, the Storage class column displays storage classes from the target package.

  1. In the Model Explorer Model Hierarchy pane, select Base Workspace.

  2. In the toolbar, click the arrow next to the Add Signal button.

  3. In the drop-down list, select Customize class lists.

  4. In the Customize class lists dialog box, select a signal class from the target package. Click OK.

  5. In the Model Explorer toolbar, click the arrow next to the Add Signal button.

  6. In the drop-down list, select the target signal class.

    An object of the target signal class appears in the base workspace. Optionally, delete this unnecessary object.

  7. Use the Model Data Editor to apply storage classes from the target package to other data items. In the Model Data Editor, in the Storage class column, the drop-down list allows you to select storage classes from the target package.

To learn how to use the Model Data Editor to configure a data interface, see Design Data Interface by Configuring Inport and Outport Blocks (Simulink Coder).

Techniques to Apply Storage Classes Programmatically

To apply a storage class to an individual data item programmatically:

  1. Create a signal or parameter data object from the target package. For example, create a Simulink.Signal object or a myPackage.Parameter object.

    myParam = Simulink.Parameter(5.2);

  2. In the nested Simulink.CoderInfo object, set the StorageClass property to 'Custom'. Then, use the CustomStorageClass property to specify the storage class.

    myParam.CoderInfo.StorageClass = 'Custom';
    myParam.CoderInfo.CustomStorageClass = 'ExportToFile';

  3. If the storage class allows you to specify additional settings such as Header file (see Specify File Names and Other Data Attributes With Storage Class (Embedded Coder)), in the nested Simulink.CoderInfo object, use the CustomAttributes property to specify values for the additional settings.

    myParam.CoderInfo.CustomAttributes.HeaderFile = 'myHeader.h';

  4. Choose one of these techniques to apply the storage class to a data item in a model:

    • To apply the storage class directly to a signal, state, or Data Store Memory block, name the signal or state. Then, use the data object you created to set the SignalObject signal property, the SignalObject block parameter (for an Outport block), or the StateSignalObject block parameter. Clear the data object you created. For an example, see Apply Storage Class Directly to Signal Line Programmatically.

    • To apply the storage class by keeping the data object you created (required for parameters), associate the data object with the data item in the model. To make this association, see Use Data Objects in Simulink Models (Simulink).

Apply Storage Class Directly to Signal Line Programmatically

This example shows how to apply a storage class directly to a signal line in a model, without an external data object.

  1. Open the example model rtwdemo_secondOrderSystem.

    rtwdemo_secondOrderSystem

  2. Create a handle to the output of the block named Force: f(t).

    portHandles = get_param('rtwdemo_secondOrderSystem/Force: f(t)','PortHandles');
    outportHandle = portHandles.Outport;

  3. Set the name of the corresponding signal to ForceSignal.

    set_param(outportHandle,'Name','ForceSignal')

  4. In the base workspace, create a signal object and specify a storage class and relevant additional settings such as HeaderFile.

    tempObj = Simulink.Signal;
    tempObj.CoderInfo.StorageClass = 'Custom';
    tempObj.CoderInfo.CustomStorageClass = 'ExportToFile';
    tempObj.CoderInfo.CustomAttributes.HeaderFile = 'myHdrFile.h';

    You can create the object from the data class package Simulink, or from any other package, such as a package that you create.

  5. Embed the signal object in the target signal line by attaching a copy of the temporary workspace object.

    set_param(outportHandle,'SignalObject',tempObj);

  6. Clear the object from the base workspace. The signal now uses an embedded copy of the object.

    clear tempObj
    

Modify Storage Class Applied Directly to Signal Line

To modify an existing embedded signal object, copy the object into the base workspace, modify the copy, and reattach the copy to the signal or state. For example, to change the storage class of the embedded signal object that you attached to the signal ForceSignal in Apply Storage Class Directly to Signal Line Programmatically:

  1. Copy the existing embedded signal object into the base workspace.

    tempObj = get_param(outportHandle,'SignalObject');

  2. Modify the properties of the object in the workspace.

    tempObj.CoderInfo.CustomStorageClass='ImportFromFile';
    tempObj.CoderInfo.CustomAttributes.HeaderFile = 'myOtherHdrFile.h';

  3. Reattach a copy of the signal object.

    set_param(outportHandle,'SignalObject',tempObj);
    clear tempObj

Organize Parameter Data into a Structure by Using the Struct Storage Class

This example shows how to use the Struct storage class to organize block parameter values into a structure in the generated code. You apply the storage class directly to individual data items in a model.

To create a custom structure of parameter data in the generated code, consider creating a corresponding structure in Simulink®. See Organize Data into Structures in Generated Code.

  1. Create the ex_struct_param model with three Constant blocks and three Outport blocks.

  2. On the Modeling tab, click Model Data Editor.

  3. In the Model Data Editor, select the Parameters tab.

  4. In the model, select the upper Constant block.

  5. In the Model Data Editor, use the Value column to set the constant value to p1.

  6. Next to p1, click the action button and select Create.

  7. In the Create New Data dialog box, set Value to Simulink.Parameter(1.0) and click Create.

    A Simulink.Parameter object named p1 appears in the base workspace.

  8. In the property dialog box for p1, click OK.

  9. Use the Model Data Editor to set the other constant values by using parameter objects named p2 (value 2.0) and p3 (value 3.0).

  10. In the Model Data Editor, set the Change view drop-down list to Code.

  11. Click the Show/refresh additional information button.

    The data table now contains rows that correspond to the new parameter objects.

  12. Use the Storage Class column to apply the storage class Struct to all three parameter objects.

  13. Use the Struct Name column to set the structure name to my_struct.

  14. Press Ctrl+B to generate code.

The file ex_struct_param.h defines a structure type, my_struct_type.

/* Type definition for custom storage class: Struct */
   typedef struct my_struct_tag {
      real_T p1;
      real_T p2;
      real_T p3;
   } my_struct_type;

The file ex_struct_param.c defines the global variable my_struct.

/* Definition for custom storage class: Struct */
my_struct_type my_struct = {
     /* p1 */
     1.0,

     /* p2 */
     2.0,

     /* p3 */
     3.0
};

Storage Class Limitations

For information about limitations, see Storage Class Limitations.

Related Topics