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.
For a list of built-in storage classes that you can choose, see Choose Storage Class for Controlling Data Representation in Generated Code.
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.
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):
To apply the storage class to a signal, state, or Data Store Memory
block (without creating a data object), configure the data item to use storage classes
from the target package instead of the Simulink
package. For
example, if the name of the target package is myPackage
, in a
Signal Properties dialog box or the Property Inspector, set Signal object
class to myPackage.Signal
. If the target class does
not appear in the list, see Target Class Does Not Appear in List of Signal Object Classes.
If you use the Model Data Editor, to identify the target package, you must use a different technique. See Apply Storage Class from Specific Package to Signal, State, or Data Store Memory Block Using the Model Data Editor.
To apply the storage class by using a data object (required for block parameters),
instead of creating a Simulink.Signal
or
Simulink.Parameter
object, create a
myPackage.Signal
or myPackage.Parameter
object. To
create data objects from your package, see Create Data Objects from Another Data Class Package (Simulink). For an example that shows how to create and apply your own storage class, see
Create and Apply a Storage Class.
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:
From the list, select Customize class lists
.
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.
From the drop-down list, select the option that corresponds to what you
selected. For example, select mpt.Signal
.
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.
In the Model Explorer Model Hierarchy pane, select Base Workspace.
In the toolbar, click the arrow next to the Add Signal
button.
In the drop-down list, select Customize class
lists
.
In the Customize class lists dialog box, select a signal class from the target package. Click OK.
In the Model Explorer toolbar, click the arrow next to the Add Signal button.
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.
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).
To apply a storage class to an individual data item programmatically:
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);
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';
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';
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).
This example shows how to apply a storage class directly to a signal line in a model, without an external data object.
Open the example model rtwdemo_secondOrderSystem
.
rtwdemo_secondOrderSystem
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;
Set the name of the corresponding signal to
ForceSignal
.
set_param(outportHandle,'Name','ForceSignal')
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.
Embed the signal object in the target signal line by attaching a copy of the temporary workspace object.
set_param(outportHandle,'SignalObject',tempObj);
Clear the object from the base workspace. The signal now uses an embedded copy of the object.
clear tempObj
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:
Copy the existing embedded signal object into the base workspace.
tempObj = get_param(outportHandle,'SignalObject');
Modify the properties of the object in the workspace.
tempObj.CoderInfo.CustomStorageClass='ImportFromFile'; tempObj.CoderInfo.CustomAttributes.HeaderFile = 'myOtherHdrFile.h';
Reattach a copy of the signal object.
set_param(outportHandle,'SignalObject',tempObj); clear tempObj
Struct
Storage ClassThis 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.
Create the ex_struct_param
model with three
Constant blocks and three Outport blocks.
On the Modeling tab, click Model Data Editor.
In the Model Data Editor, select the Parameters tab.
In the model, select the upper Constant block.
In the Model Data Editor, use the Value column to set the
constant value to p1
.
Next to p1
, click the action button and select Create.
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.
In the property dialog box for p1
, click
OK.
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
).
In the Model Data Editor, set the Change view drop-down list
to Code
.
Click the Show/refresh additional information button.
The data table now contains rows that correspond to the new parameter objects.
Use the Storage Class column to apply the storage class
Struct
to all three parameter
objects.
Use the Struct Name column to set the structure name to
my_struct
.
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 };
For information about limitations, see Storage Class Limitations.