Class: matlab.DiscreteEventSystem
Package: matlab

Event action when entity iterates




[entity,events,next]=iterate(obj,storage,entity,tag,cur) specifies event actions for when an entity is processed as a part of an iterate event.

[entity,events,next,out1,...]=iterate(obj,storage,entity,tag,cur,in1,...) specifies such event actions when the block has one or more input signal ports and/or signal output ports.

Input Arguments

expand all

Discrete-event System object.

Index of the storage element.

Entity currently being processed. Entity has these fields:

  • sys (MATLAB structure) — It has these fields:

    • id (double) — Entity ID

    • priority (double) — Entity priority

  • data — Entity data

Tag of the current entity iterate event.

MATLAB structure indicating current state of iteration. The structure has these fields:

  • size

    Total number of entities the storage has

  • position

    Position of the current iterating entity

Any data inputs of the object. These input arguments exist only when the object has data inputs.

Output Arguments

expand all

Entity being processed, possibly with changed data.

Events to be scheduled after the method returns. Use matlab.DiscreteEventSystem class methods to create events. Each event has these fields:

  • type (character vector) — Type of the event

  • delay (double) — Delay before the event

  • priority (double) — Priority of the event

  • Storage (double) — Index of the storage element

  • tag (character vector) — Event tag

  • location (MATLAB structure) — Source or destination location of entity

  • True

    Continue to process the next entity in the storage element.

  • False

    Terminate the iterate event, and leave the rest of the entities of the storage element unprocessed.

Data outputs of the object. You must specify these output arguments when the object has data outputs.


Forward the First Entity

Forward the first entity with matching data value to output port 1 of the discrete-event system.

function [entity,events,next] = iterate(obj,storage,entity,tag,status)
    % Forward the first entity with matching data value to output
    % port 1 of the discrete-event system.
    disp(['Searching in storage element ' num2str(storage)]);
    disp(['    Total size = ' num2str(status.size)]);
    disp(['    Current position = ' num2str(status.position)]);
    if ( == obj.dataToSearch)
        events = obj.eventForward('output', 1, 0);
        next = false;   % Found -- early terminate
        events = [];
        next = true;    % Not yet found -- continue

Custom Entity Storage Block with Iteration Event

In this example, a custom block allows entities to enter its storage element through its input port. The storage element is a priority queue that sorts the entities based on their Diameter attribute in ascending order. Every entity entry to the block's storage invokes an iteration event to display the diameter and the position of each entity in the storage.

For more information, see Create a Custom Entity Storage Block with Iteration Event.

classdef CustomEntityStorageBlockIteration < matlab.DiscreteEventSystem
    % A custom entity storage block with one input port and one storage element. 
    % Nontunable properties 
    properties (Nontunable)
        % Capacity
        Capacity = 5;
    % Create the storage element with one input and one storage.
    methods (Access=protected)

        function num = getNumInputsImpl(obj)
            num = 1;
        function num = getNumOutputsImpl(obj)
            num = 0;
        function entityTypes = getEntityTypesImpl(obj)
            entityType1 = obj.entityType('Wheel');
            entityTypes = entityType1;
        function [inputTypes,outputTypes] = getEntityPortsImpl(obj)
            inputTypes = {'Wheel'};
        function [storageSpecs, I, O] = getEntityStorageImpl(obj)
            storageSpecs = obj.queuePriority('Wheel',obj.Capacity, 'Diameter','ascending');
            I = 1;
            O = [];
    % Entity entry event action

        function [entity, event] = WheelEntry(obj,storage,entity, source)
            % Entity entry invokes an iterate event.      
            event = obj.eventIterate(1, '');

        % The itarate event action
        function [entity,event,next] = WheelIterate(obj,storage,entity,tag,cur)
            % Display wheel id, position in the storage, and diameter.
            fprintf('Wheel id %d, Current position %d, Diameter %d\n', ...
      , cur.position,;
            if cur.size == cur.position 
                fprintf('End of Iteration \n')
            next = true;


Introduced in R2016a