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.
To open the example, see Example
Model for Noncumulative Entity Count
.
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.
Connect the blocks as shown in the diagram.
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
.
Double-click the function signature on the Simulink Function block
and enter nonCumCount()
.
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.
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
Connect the output of the MATLAB Function block to a Simulink® Scope block.
In the parent model, double-click the Entity Terminator block. In the Entry action field of the Event actions tab, enter this code.
nonCumCount();
Simulate the model and open the Scope block in the Simulink Function block.
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.
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.
To count the number of events that occur instantaneously, use
nonCumCount()
in any Event actions.
Entity Gate | Entity Generator | Entity Input Switch | Entity Terminator