How to generate entity using the data from another entity in discrete system event?

Hi everyone,
I was hoping to do something which I thought would be relatively simple but had been struggling, also we are new to discrete events in simulink/matlab so might have missed where the answer lies :)
Using matlab.DiscreteEventSystem we would like to generate an entity Type A, whenever another entity Type B enters the storage, but using some (and modified) attributes of the entity Type B. Generating an entity Type A with default values based on the entry event of entity Type B has been simple using eventGenerate for Entity A within an Entry event function for Entity B. Can also modify and set the values of the fields of the new generated Type A within its Generate to specific values. However, can't figure out how to pass the values of the attributes of the entity Type B that triggered the Entry to the Generate of Type A.

1 Comment

Actually, figured there was a similar question here which @Abdolkarim Mohammadi has partially answered which is exactly what we were trying to do but need a bit more clarification how to do the property setting.
https://uk.mathworks.com/matlabcentral/answers/829623-simevents-setting-entity-attributes-based-on-other-entities?s_tid=answers_rc1-1_p1_MLT

Sign in to comment.

 Accepted Answer

Okay, here is the solution to the above after a bit more struggle, in case anyone elses faces similar challenges (without the complete MDSE fields necessary as I thought those would be intuitive - see the Delay demo, note you need to create two storages for two different types of entities you have).
Feel free to reach out if in doubt.
classdef myFunc < matlab.DiscreteEventSystem
% Define the variables you want to transfer between the entities as properties
properties
a = 0
end
% Define setters and getters for your property
function getVal = get.a(obj)
getVal = obj.a;
end
function obj = set.a(obj, val)
obj.sqnc = val;
end
% Define the Entry object for your first entity which you want to transfer
% values from
function [entity,events] = EntityTypeBEntry(obj,storage,entity,source)
obj.a = entity.data.attributeToTransfer;
% I use eventDestroy after creating the generate event, 2 points to the
% second storage where Entity Type A is generated, as the first one is used by
% Entity Type B - 0.001s and 300priority are arbitrary values
events = [obj.eventGenerate(2, 'anyTag', 0.001, 300),...
obj.eventDestroy()];
end
function [entity,event] = ent_UDPPayloadGenerate(obj,storage,entity,source)
% Specify event actions when entity enters storage.
entity.data.attributeToTransferTo = obj.a;
% Forward the event to the output ('output',1,) after its values are set without
% delay (,,0)
event = obj.eventForward('output',1 ,0);
end

More Answers (0)

Categories

Find more on Discrete-Event Simulation in Help Center and File Exchange

Products

Release

R2024b

Asked:

on 23 Jun 2025

Edited:

on 23 Jun 2025

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!