| SimEvents® | ![]() |
| On this page… |
|---|
To manipulate attributes via 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 block, see
Distributed Processing for Multi-Class Jobs demo, within the Distribution Center subsystem and its Service History Monitor subsystem
The Attribute Function block has one entity input port and one entity output port. The block manipulates attributes of each arriving entity. 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.
Argument-Naming Rules
|
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 called fcn uses the value of the attribute called Attribute1 to compute values of the attributes called Attribute1 and Attribute2.
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 dimension and double data type, but cannot be a structure or frame. For each attribute, the dimensions and complexity must be consistent throughout the model. Also, the function must use the Embedded MATLAB™ subset. For more information, see Overview of the Embedded MATLAB Subset in the Embedded MATLAB 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 directory because the application creates files there. |
Suppose you already have an M-file function that represents your desired attribute manipulation and that uses the Embedded MATLAB subset. The function might or might not satisfy the argument-naming rules for the Attribute Function block. This example illustrates a technique that can help you satisfy the naming rule while preserving your legacy code.
Follow Argument-Naming Rule at Top Level
The top level of the function associated with the Attribute Function block must satisfy the argument-naming rule. In this example, the function reads the entity's x attribute, so the function's input argument must be called x. The function assigns a new value to the entity's x attribute, so the function's output argument must be called out_x.
function out_x = update_x_attribute(x)
Invoke Legacy Code
Using the variable names x and out_x, invoke your legacy code. The usage below assumes that you have called the function mylegacyfcn.
out_x = mylegacyfcn(x);
Include Legacy Code
Include your legacy code as either a subfunction or an extrinsic function; see Calling Functions in the Embedded MATLAB Subset in the Embedded MATLAB documentation for details.
In your legacy code, the function name and the number of input and output arguments must be compatible with the invocation above. However, the legacy code does not need to follow the argument-naming rules for the Attribute Function block. Below is an example of a function that is compatible with the invocation above.
function outp = mylegacyfcn(inp)
if inp < 0
outp = 0;
else
if ((inp >= 0) && (inp < 1))
outp = 0.25;
else
outp = 5;
end
endTo manipulate attributes using blocks to perform the computation, use an arrangement as in the figure below. You can vary the number of outputs of the Get Attribute block, the number of inputs of the Set Attribute block, or the connections or contents of the Discrete Event Subsystem block.
Top-Level Model

Subsystem Contents

The key components are in the table.
| Block | Role |
|---|---|
| Get Attribute | Queries the entity for its attribute value. |
| Discrete Event Subsystem | Ensures correct timing of the computation inside the subsystem, given that the output signal from the Get Attribute block is an event-based signal. Other signals, if any, in the computation can be event-based or time-based. To learn more about discrete event subsystems, see Controlling Timing with Subsystems. |
| Content of the discrete event subsystem | Models your specific computation. The figure above shows one example: 5|Attr+t|, where Attr is an attribute value and t is a signal value unrelated to attributes. The subsystem is configured to execute if and only if Attr has a sample time hit. |
| Single Server with Service time set to 0 | 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. |
![]() | Setting Attributes of Entities | Accessing Attributes of Entities | ![]() |
| © 1984-2008- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |