Main Content

Noncumulative Entity Count

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.

Explore the Model

Open the example model noncumulative_count. The model contains two Entity Generator blocks, an Entity Input Switch block, an Entity Terminator block, and a Simulink Function block from the SimEvents® library. For more information about Simulink Function blocks, see Simulink Function.

Double-click the Entity Generator1 block to open the Block Parameters dialog box. In the Entity generation tab, Period is set to 2. In the model, two entities arrive to Entity Terminator block at times 0, 2, 4, 6, 8, and 10, and one entity arrives at times 1, 3, 5, 7, and 9. Double-click the Simulink Function block.

The Simulink Function block contains a Digital Clock block from the Simulink > Sources library. The Sample time parameter is set to -1 for inherited sample time. The MATLAB Function block within the Simulink Function block contains code that counts the number of noncumulative entities. Double-click the MATLAB Function block to view the 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

The output of the MATLAB Function block is connected to a Simulink® Scope block.

In the parent model, double-click the Entity Terminator to open the Block Parameters dialog box. The Entry action box of the Event actions tab specifies the nonCumCount function.

Observe Noncumulative Entity Count

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

2. Change the plotting settings of the Scope block by selecting Settings > Style from the Scope tab. In the Line Style section, select NONE from the Style list and the circle symbol from the Marker list.

3. The scope shows the noncumulative entity count for the entities arriving the Entity Terminator block. The block also illustrates the instantaneous entity arrivals at each time unit.

To count the number of events that occur instantaneously, use nonCumCount in an event action.

See Also

| | |

Topics