Main Content

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

  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.

    Block diagram showing an Entity Generator block connected to a MATLAB Discrete-Event System with System Object name CustomEntityStorageBlockSLFunc that, in turn, connects to an Entity Terminator block. A Simulink Function block with interface y=AssignData() also connects to the MATLAB Discrete-Event System block. The Entity Generator block connects to a Scope block.

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

      Simulink Function block y=assignData() showing a Uniform Random Number block connected to the output.

    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.

    Scope block representing number of entities departing the Entity Generator block, graphically.

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

    Diagnostic Viewer displaying the Entity Attribute Values assigned to three entities in the form of printed text. In order, the values are: -0.562082, -0.905911 and 0.357729.

See Also

| | | | |

Related Topics