How to predefine no.of entites in a entity_batch_creator_block in simevents
4 views (last 30 days)
Show older comments
I want entity_batch_creator block to take no. of entities from its previous block without explicitly defining it in the dialog box.
That is if for a specific time period no of entities generated are 20 then the entity batch creator takes the batch of 20 entities automatically.
1 Comment
Answers (1)
Altaïr
on 3 Jun 2025
Edited: Altaïr
on 4 Jun 2025
The default Entity Batch Creator block is designed for constant batch sizes. To handle variable batch sizes, you'll need to create a custom block by inheriting from matlab.DiscreteEventSystem and include it in Simulink via the MATLAB Discrete-Event System block. For details on creating such custom blocks, refer to:
web(fullfile(docroot, 'simevents/ug/create-custom-blocks-using-matlab-discrete-event-system-block.html'))
The core logic involves two steps:
- Queue entities in FIFO storage until the current batch size is reached
- Release all entities immediately once the batch size is met (with no inter-entity delay)
The DynamicBatchCreator.m file provides an example implementation where:
- Batch size is a time-varying input signal
- Entities require a batchSizeAttrib attribute which stores the batch size value in the block when the enitity enters the custom block
- The InputEntityEntry and InputEntityIterate functions capture the main logic to keep in track of the number of entities in storage and outputting the entities once the batch size is reached.
Note that the input entities are assumed to be of InputEntity type with an attribute named batchSizeAttrib.
function [entity, events] = InputEntityEntry(obj, storage, entity, source, batchSize)
% Increment entity count
obj.EntityCount = obj.EntityCount + 1;
% Get current batch size from signal input (port 2)
batchSize = round(batchSize);
% Ensure batch size is at least 1
if batchSize < 1
batchSize = 1;
end
% Set batchSizeAttrib attribute of the entity
entity.data.batchSizeAttrib = batchSize;
% Entity storage service delay timer
event1 = obj.eventTimer('DelayTimer', 0);
% Check if we've reached batch size
if obj.EntityCount == batchSize
% Reset counter
obj.EntityCount = 0;
% Output batch entity
event2 = obj.eventIterate(1, '');
events = [event1, event2];
else
events = event1;
end
end
function [entity,event,next] = InputEntityIterate(obj,storage,entity,tag,cur,batchSize)
next = true;
event = obj.eventForward('output', 1, 0.0);
end
How it works:
- InputEntityEntry triggers eventIterate when batch size is reached
event2 = obj.eventIterate(1, '');
- InputEntityIterate releases entities with eventForward
- The sample model shown below uses a step input to change batch size dynamically (from 2→3 at t=5s)

The logged signal from Simulation Data Inspector shown below confirms that the custom batch creator operates as expected:

For more information on the methods supported by the matlab.DiscreteEventSystem class use the command below:
web(fullfile(docroot, 'simevents/ref/matlab.discreteeventsystem-class.html'))
0 Comments
See Also
Categories
Find more on Discrete-Event Simulation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!