Documentation

Apply Custom Storage Classes to Individual Signal, State, and Parameter Data Elements

InApply 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 usecustom storage classesfor 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 custom storage classes from the built-in package万博1manbetx.

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

Built-In Custom Storage Classes

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

创建您自己的自定义存储类

If the custom storage classes from the万博1manbetxpackage do not satisfy your requirements, you can define your own custom storage classes. For information about defining your own custom storage class, seeCreate Code Definitions to Override Default Settings.

Techniques to Apply Custom Storage Classes Interactively

To directly apply a custom storage class from the built-in package万博1manbetxinteractively, use the same techniques that you use to directly apply standard storage classes, described inApply Storage Classes to Individual Signal, State, and Parameter Data Elements.

To apply a custom storage class from a package other than万博1manbetx(such as a package that you create):

Target Class Does Not Appear in List of Signal Object Classes

When you use theSignal object classdrop-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, selectCustomize class lists.

  2. In the dialog box, underSignal classes, select the check box next to the class that you want. For example, to use custom storage classes from the built-in packagempt, select the check box next tompt.Signal. ClickOK.

    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, selectmpt.Signal.

Apply Custom Storage Class from Specific Package to Signal, State, orData Store MemoryBlock Using the Model Data Editor

When you use the Model Data Editor, to apply custom 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, theStorage classcolumn displays custom storage classes from the target package.

  1. In the Model ExplorerModel Hierarchypane, selectBase Workspace.

  2. In the toolbar, click the arrow next to theAdd Signalbutton.

  3. In the drop-down list, selectCustomize class lists.

  4. In theCustomize class listsdialog box, select a signal class from the target package. ClickOK.

  5. In the Model Explorer toolbar, click the arrow next to theAdd Signalbutton.

  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 custom storage classes from the target package to other data items. In the Model Data Editor, in theStorage classcolumn, the drop-down list allows you to select custom storage classes from the target package.

To learn how to use the Model Data Editor to configure a data interface, see设计数据接口的配置尺寸和Outport Blocks(Simulink Coder).

Techniques to Apply Custom Storage Classes Programmatically

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

  1. Create a signal or parameter data object from the target package. For example, create aSimulink.Signalobject or amyPackage.Parameterobject.

    myParam = Simulink.Parameter(5.2);

  2. In the nestedSimulink.CoderInfoobject, set theStorageClassproperty to'Custom'. Then, use theCustomStorageClassproperty to specify the custom storage class.

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

  3. If the custom storage class allows you to specify additional settings such asHeader file(seeSpecify File Names and Other Data Attributes With Storage Class (Embedded Coder)), in the nestedSimulink.CoderInfoobject, use theCustomAttributesproperty to specify values for the additional settings.

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

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

    • To apply the custom storage class directly to a signal, state, orData Store Memoryblock, name the signal or state. Then, use the data object you created to set theSignalObjectsignal property, theSignalObjectblock parameter (for anOutportblock), or theStateSignalObjectblock parameter. Clear the data object you created. For an example, seeApply Custom Storage Class Directly to Signal Line Programmatically.

    • To apply the custom 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, seeUse Data Objects in Simulink Models(Simulink).

Apply Custom Storage Class Directly to Signal Line Programmatically

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

  1. Open the example modelrtwdemo_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 toForceSignal.

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

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

    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万博1manbetx, 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.

    cleartempObj

Modify Custom 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 custom storage class of the embedded signal object that you attached to the signalForceSignalinApply Custom 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); cleartempObj

Organize Parameter Data into a Structure by Using theStructCustom Storage Class

This example shows how to use theStructcustom 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®. SeeOrganize Data into Structures in Generated Code.

  1. Create theex_struct_parammodel with threeConstantblocks and threeOutportblocks.

  2. In the model, selectView>Model Data Editor.

  3. In the Model Data Editor, select theParameterstab.

  4. In the model, select the upperConstantblock.

  5. In the Model Data Editor, use theValuecolumn to set the constant value top1.

  6. Next top1, click the action buttonand selectCreate.

  7. In the Create New Data dialog box, setValuetoSimulink.Parameter(1.0)and clickCreate.

    ASimulink.Parameterobject namedp1appears in the base workspace.

  8. In the property dialog box forp1, clickOK.

  9. Use the Model Data Editor to set the other constant values by using parameter objects namedp2(value2.0) andp3(value3.0).

  10. In the Model Data Editor, set theChange viewdrop-down list toCode.

  11. Click theShow/refresh additional informationbutton.

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

  12. Use theStorage Classcolumn to apply the custom storage classStructto all three parameter objects.

  13. Use theStruct Namecolumn to set the structure name tomy_struct.

  14. PressCtrl+Bto generate code.

The fileex_struct_param.hdefines 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 fileex_struct_param.cdefines the global variablemy_struct.

/* Definition for custom storage class: Struct */ my_struct_type my_struct = { /* p1 */ 1.0, /* p2 */ 2.0, /* p3 */ 3.0 };

Custom Storage Class Limitations

For information about limitations, seeStorage Class Limitations.

Related Topics

Was this topic helpful?