Main Content

Noncumulative Counting of Entities

This example shows how to count entities, which arrive to an Entity Terminator block, in a noncumulative way by resetting the counter at each time instant.

Snapshot of a Simulink model that shows two Entity Generator blocks, Entity Generator and Entity Generator1 connected to one Entity Switch block that, in turn, connects to an Entity Terminator block. Output from a Simulink Function block is also linked to the Entity Terminator block.

To open the example, see Example Model for Noncumulative Entity Count.

  1. Add two Entity Generator blocks, an Entity Input Switch block, an Entity Terminator block, and a Simulink Function block from the SimEvents® library to a new model. For more information, see Simulink Function.

  2. Connect the blocks as shown in the diagram.

  3. Double-click the Entity Generator1 block. In the Entity generation tab, set the Period to 2.

    In the model, 2 entities arrive to Entity Terminator block at time 0, 2, 4, 6, 8, 10 and 1 entity arrives at time 1, 3, 5, 7, 9.

  4. Double-click the function signature on the Simulink Function block and enter nonCumCount().

    Snapshot of the Simulink Function block nonCumCount that contains a Digital Clock connected to a MATLAB Function block that, in turn, connects to a Scope.

  5. Double-click the Simulink Function block. Add a Digital Clock block from the Simulink > Sources library. Set the Sample time parameter to -1 for inherited sample time.

  6. Add a MATLAB Function block. Double-click it and enter this code.

    function y = fcn(curtime)
    % Define count for counting and prevtime for previous time stamp
    persistent count prevtime;
    % Check if prevtime is empty and initiate the count
    if isempty(prevtime)
        prevtime = curtime;
        count = 0;
    end
    % Increase count by 1 for equal time stamps.
    if isequal(curtime, prevtime)
        count = count + 1;
    % Reset count to 1 if two consequitive time stamps are not identical  
    else
        prevtime = curtime;
        count = 1;
    end
    % Output count for visualization
    y = count;
    end
    Save the file (optional).

  7. Connect the output of the MATLAB Function block to a Simulink® Scope block.

  8. In the parent model, double-click the Entity Terminator block. In the Entry action field of the Event actions tab, enter this code.

    nonCumCount();

  9. Simulate the model and open the Scope block in the Simulink Function block.

  10. Change the plotting settings of the Scope block by right-clicking the plot and selecting Style. Select no line for the Line and circle for the Marker parameters.

  11. Observe that the block illustrates the noncumulative entity count for the entities arriving the Entity Terminator block. The block also illustrates the instantaneous entity arrivals at each time.

    Scope block output representing the noncumulative entity count arriving at the Entity Terminator block, graphically. Numbers are marked using circular symbols.

To count the number of events that occur instantaneously, use nonCumCount() in any Event actions.

See Also

| | |

Related Examples

More About