Call Simulink Function from a MATLAB Discrete-Event System Block
This example shows how to call a Simulink® function when an entity enters the storage element of a custom discrete-event system block, and to modify entity attributes. For more information about calling Simulink functions from MATLAB System block, see Call Simulink Functions from MATLAB System Block.
To represent this behavior, a custom block is generated with one input, one output, and one storage element. For more information about creating a custom entity storage block, see Delay Entities with a Custom Entity Storage Block.
See the Code that Calls Simulink Function to Modify Entity Attributes
Modify Entity Attributes
Define the name of the Simulink function to be called in the discrete-event System object™ using the
getSimulinkFunctionNamesImpl
method.function name = getSimulinkFunctionNamesImpl(obj) % Declare the name of the Simulink Function. name = {'assignData'}; end
The name of the Simulink function is declared as
assignData
.Call
assignData
in the entry event action.function [entity,event] = CarEntry(obj,storage,entity,source) % Assign data when an entity enters the storage. entity.Attribute1 = assignData(); coder.extrinsic('fprintf'); fprintf('Entity Attribute Value: %f\n', entity.Attribute1); event = obj.eventForward('output', 1, obj.Delay); end
Build the Model
Create a model using an Entity Generator block, MATLAB Discrete-Event System block, and an Entity Terminator block.
Open the MATLAB Discrete-Event System block, and set the Discrete-event System object name to
CustomEntityStorageBlockSLFunc
.Output the Number of entities departed, d statistic from the Entity Generator block and connect it to a scope.
Add a Simulink Function block to your model.
On the Simulink Function block, double-click the function signature and enter
y = assignData()
.In the Simulink Function block, add a Uniform Random Number block and change its Sample time parameter to
-1
.
Simulate the model. The scope displays
3
entities departed the Entity Generator block.The Diagnostic Viewer displays the random attribute values assigned to
3
entities when they enter the storage.
See Also
matlab.DiscreteEventSystem
| entry
| matlab.System
| getEntityStorageImpl
| getEntityPortsImpl
| getEntityTypesImpl