Wait for new ROS message
statustext] = receive(___)
status indicating whether a message has
been received successfully, and a
captures additional information about the
using any of the arguments from the previous syntaxes. If an error
condition occurs, such as no message received within the specified
status will be
false, and this function will not display
Create A Subscriber and Get Data From ROS
Connect to a ROS network. Set up a sample ROS network. The
'/scan' topic is being published on the network.
Launching ROS Core... Done in 0.57433 seconds. Initializing ROS master on http://172.29.209.44:58816. Initializing global node /matlab_global_node_40158 with NodeURI http://dcc741990glnxa64:37823/ and MasterURI http://localhost:58816.
Create a subscriber for the
'/scan' topic using message structures. Wait for the subscriber to register with the master.
sub = rossubscriber('/scan','DataFormat','struct'); pause(1);
Receive data from the subscriber as a ROS message structure. Specify a 10-second timeout.
[msg2,status,statustext] = receive(sub,10)
msg2 = struct with fields: MessageType: 'sensor_msgs/LaserScan' Header: [1x1 struct] AngleMin: -0.5467 AngleMax: 0.5467 AngleIncrement: 0.0017 TimeIncrement: 0 ScanTime: 0.0330 RangeMin: 0.4500 RangeMax: 10 Ranges: [640x1 single] Intensities: 
status = logical 1
statustext = 'success'
Shutdown the timers used by sample network.
Shut down ROS network.
Shutting down global node /matlab_global_node_40158 with NodeURI http://dcc741990glnxa64:37823/ and MasterURI http://localhost:58816. Shutting down ROS master on http://172.29.209.44:58816.
msg — ROS message
Message object handle | structure
ROS message, returned as a
object handle or structure.
In a future release, ROS Toolbox will use message structures instead of objects for ROS messages.
To use message structures now, set the
"struct". For more information, see ROS Message Structures.
status — Status of the message reception
Status of the message reception, returned as a
logical scalar. If no message
is received, status will be
status output argument when you use receive in the
entry-point function for code generation. This will avoid runtime errors and outputs the
status instead, which can be reacted to in the calling code.
statustext — Status text associated with the message reception status
Status text associated with the message reception, returned as one of the following:
'success'— The message was successfully received.
'timeout'— The message was not received within the specified timeout.
'unknown'— The message was not received due to unknown errors.
For code generation:
statusoutput argument when you call
receivein the entry-point function. This will avoid runtime errors and instead, outputs the status of message reception, which can be reacted to in the calling code.
C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.
Usage notes and limitations:
Supported only for
To monitor the message reception status and react in the calling code, use the
statusoutput argument. This will avoid runtime errors when no message is received.
Version HistoryIntroduced in R2019b
R2021a: ROS Message Structures
You can now create messages as structures with fields matching the message object properties. Using structures typically improves performance of creating, updating, and using ROS messages, but message fields are no longer validated when set. Message types and corresponding field values from the structures are validated when sent across the network.
To use ROS messages as structures, use the
argument when creating your publishers, subscribers, or other ROS objects. Any messages
generated from these objects will utilize structures.
pub = rospublisher("/scan","sensor_msgs/LaserScan","DataFormat","struct") msg = rosmessage(pub)
You can also create messages as structures directly, but make sure to specify the data
"struct" for the publisher, subscriber, or other ROS objects as
well. ROS objects still use message objects by default.
msg = rosmessage("/scan","sensor_msgs/LaserScan","DataFormat","struct") ... pub = rospublisher("/scan","sensor_msgs/LaserScan","DataFormat","struct")
In a future release, ROS messages will use structures by default and ROS message objects will be removed.
For more information, see Improve Performance of ROS Using Message Structures.