| Contents | Index |
| On this page… |
|---|
Choice of Approaches for Manipulating Attributes |
The table compares approaches for manipulating attributes of entities.
| Approach | Advantages | Details |
|---|---|---|
| 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. |
| Writing Functions to Manipulate Attributes |
| 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. |
| Using Block Diagrams to Manipulate Attributes |
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 block, see
Modeling Multicomponent Parts using Combiners and Splitters demo
Distributing Multi-Class Jobs to Service Stations 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. 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.
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 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 Code Generation and MATLAB Language Features Not Supported for Code Generation in the code generation 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 application creates files there. |
Suppose you already have a file that defines a function that:
Represents your desired attribute manipulation
Uses only those MATLAB functions and operators that are suitable for code generation
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 for Code Generation in the code generation 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 following figure.
Top-Level Model

Subsystem Contents

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.
| Block | Role |
|---|---|
| Get Attribute | Queries the entity for its attribute value. |
| Atomic Subsystem | Ensures that the computation executes as an atomic unit. To learn more, see Performing Computations in Atomic Subsystems. |
| Content of the 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 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 | ![]() |

Model electronic system architectures, process flows, and logistics as queuing systems or agent-based systems.
Get free kit| © 1984-2012- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |