Main Content

Process Message Payload Using MATLAB System Block

This example shows how to send, receive, and process messages using the MATLAB System block. Use System objects to author blocks to model custom behavior to send and receive messages and manipulate the message payload.

Load and Open the Model

Open the model of the Message Send and Receive system. This model contains a random number generator as a data source. Based on that data, the Message Sender sends a message with a sine wave payload to a queue block. The queue block stores the messages, and the Message Receiver converts the message back to data.

open_system('slexMessageArrivalExample');

UsegetInterfaceImplSystem Object API to Specify Message Ports

getInterfaceImplis used in this example to specify the input and output message ports in the MATLAB System block for both the Message Sender and the Message Receiver. For the Message Sender,getInterfaceImplis used to define the output of the MATLAB System block as a message. This action prompts the System object to create a message output. For the Message Receiver,getInterfaceImplis used to define the input of the System object as a message and the output as data.

%Function to send messagesfunctioninterface = getInterfaceImpl(~) importmatlab.system.interface.*; interface = Output("Out1", Message);end
%Function to receive messages and output as datafunctioninterface = getInterfaceImpl(obj) importmatlab.system.interface.*; interface = [Input("In1", Message),...Output("Out1", Data), Output("out2", Data)];end

Set the Propagators and Sample time

以下四个繁殖专家需要设置在the Message Sender:getOutputSizeImpl,getOutputDataTypeImpl,isOutputComplexImpl,isOutputFixedSizeImpl.

In this example, the message queue has a maximum capacity of 16 messages. The random number generator has a sample time of 0.1. The receiver has a sample time of 1. The Message Receiver sample time is set in the MATLAB System block using thegetSampleTimeImplAPI.

functionsts = getSampleTimeImpl(obj) sts = createSampleTime(obj,'Type','Discrete',...'SampleTime',obj.SampleTime);end

Simulate the Model and Review Results

The Scope block displays the results. These results show that as you run more simulations, the random number generator produces a number greater than zero 50% of the time, as expected.