The table describes some ways that you can use data that you have attached to an entity.
|Use Attribute Values To...||Technique|
|Create a signal|
Use the Get Attribute block.
For example, see the subsystem of the model described in Lesson 3: Add Event-Based Behavior.
|Create a plot|
Use the Attribute Scope block and name the attribute in the Y attribute name parameter of the block.
Alternatively, use the X-Y Attribute Scope block and name two attributes in the X attribute name and Y attribute name parameters of the block.
For example, see the reference page for the X-Y Attribute Scope block.
|Compute a different attribute value|
Use the Attribute Function block.
For example, see Unique and Accessible Attribute Names in Composite Entity.
|Help specify behavior of a block that supports the use of attribute values for block parameters. Examples are the service time for a server and the selected port for an output switch.|
Name the attribute in the block dialog box as applicable.
For example, see Use an Attribute to Select an Output Port.
Tip Suppose that your entity possesses an attribute with one of these quantities:
When an entity with one of these attribute quantities arrives at a server or switch block, you can directly reference the attribute using an option on the server or switch block dialog box. This approach is better than creating a signal with the attribute value and ensuring that the signal is up-to-date when the entity arrives. For a comparison of the two approaches, see Use a Signal or an Attribute.
The table compares options for manipulating attributes of entities.
|Use the Get Attribute, Set Attribute, and Single Server blocks to structure the computation correctly. Use blocks inside the subsystem to perform the specific computation.||Use Block Diagrams to Manipulate Attributes|
|Write a function that uses MATLAB® code to modify an attribute or define a new attribute. Use the Attribute Function block to invoke your function for each entity that arrives at the block.||Write Functions to Manipulate Attributes|
To manipulate attributes using blocks to perform the computation, use an arrangement as in the following figure.
In your own model, you can vary:
The number of outputs of the Get Attribute block
The number of inputs of the Set Attribute block
The connections or contents of the subsystem, where all input signals of the subsystem are event-based signals
The key components are in the table.
|Get Attribute||Queries the entity for its attribute value.|
|Atomic Subsystem||Ensures that the computation executes as an atomic unit. To learn more, see Perform Computations in Atomic Subsystems.|
|Content of Atomic Subsystem|
Models your specific computation.
The earlier figure shows one example: 5|Attr+x|, where Attr is an attribute value and x is a time-based signal converted into an event-based signal. The subsystem executes if and only if Attr has a sample time hit.
|Single Server with Service time set
to ||Ensures that the Set Attribute block uses the up-to-date results of the computation. For details, see Interleaving of Block Operations.|
|Set Attribute||Assigns new value to the attribute.|
To manipulate attributes using code, use the Attribute Function block. The block lets you access existing attributes of the arriving entity, modify the values of existing attributes, or create new attributes.
For examples that use the Attribute Function block, see
The Attribute Function block has one entity input port and one entity output port. The block manipulates attributes of each arriving entity. See Attribute Value Support for the characteristics of attribute values. To edit the computation, use this procedure:
Display the block's associated function in an editor window by double-clicking the Attribute Function block.
Write the first line of the function using arguments that reflect the attributes that the function manipulates. The arguments do not reflect input or output signals.
The entity must possess any attributes named as input arguments of the function. If the entity does not possess an attribute named using an output argument of the function, then the block creates the attribute.
The default function definition, below, indicates that the function
fcn uses the value of the attribute called
compute values of the attributes called
function [out_Attribute1, out_Attribute2] = fcn(Attribute1)
Write the function to implement your specific computation. The value of each attribute can be a real- or complex-valued array of any fixed dimension and double data type, but cannot be a structure. For each attribute, the dimensions and complexity must be consistent throughout the model. Also, the function must use only those MATLAB functions and operators that are suitable for code generation. For more information, see MATLAB Language Features Supported for C/C++ Code Generation in the Simulink® documentation.
Note: If you try to update the diagram or run the simulation for a model that contains the Attribute Function block, then you must have write access to the current folder because the software creates files there.