Main Content

iterate

Class: matlab.DiscreteEventSystem
Namespace: matlab

Event action when entity iterates

Syntax

[entity,events,next]=iterate(obj,storage,entity,tag,cur)
[entity,events,... next,out1,...]=iterate(obj,storage,entity,tag,cur,in1,...)

Description

[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.

Examples

expand all

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 (entity.data == obj.dataToSearch)
        events = obj.eventForward('output', 1, 0);
        next = false;   % Found -- early terminate
    else
        events = [];
        next = true;    % Not yet found -- continue
    end
end

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;
    end
    % Create the storage element with one input and one storage.
    methods (Access=protected)

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

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

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

end

Version History

Introduced in R2016a