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 (Simulink).

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

  1. 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.

  2. 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

  1. Create a model using an Entity Generator block, MATLAB Discrete-Event System block, and an Entity Terminator block.

  2. Open the MATLAB Discrete-Event System block, and set the Discrete-event System object name to CustomEntityStorageBlockSLFunc.

  3. Output the Number of entities departed, d statistic from the Entity Generator block and connect it to a scope.

  4. Add a Simulink Function block to your model.

    1. On the Simulink Function block, double-click the function signature and enter y = assignData() .

    2. In the Simulink Function block, add a Uniform Random Number block and change its Sample time parameter to -1.

  5. Simulate the model. The scope displays 3 entities departed the Entity Generator block.

  6. The Diagnostic Viewer displays the random attribute values assigned to 3 entities when they enter the storage.

See Also

| | | | |

Related Topics