Main Content


Wait for new ROS message



msg = receive(sub) waits for MATLAB® to receive a topic message from the specified subscriber, sub, and returns it as msg.

msg = receive(sub,timeout) specifies in timeout the number of seconds to wait for a message. If a message is not received within the timeout limit, the software produces an error.


collapse all

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.79397 seconds.
Initializing ROS master on
Initializing global node /matlab_global_node_95936 with NodeURI http://bat800205glnxa64:41133/

Create a subscriber for the '/scan' topic using message structures. Wait for the subscriber to register with the master.

sub = rossubscriber('/scan','DataFormat','struct');

Receive data from the subscriber as a ROS message structure. Specify a 10-second timeout.

msg2 = receive(sub,10)
msg2 = struct with fields:
       MessageType: 'sensor_msgs/LaserScan'
            Header: [1x1 struct]
          AngleMin: -0.5216
          AngleMax: 0.5243
    AngleIncrement: 0.0016
     TimeIncrement: 0
          ScanTime: 0.0330
          RangeMin: 0.4500
          RangeMax: 10
            Ranges: [640x1 single]
       Intensities: []

Shutdown the timers used by sample network.


Shut down ROS network.

Shutting down global node /matlab_global_node_95936 with NodeURI http://bat800205glnxa64:41133/
Shutting down ROS master on

Input Arguments

collapse all

ROS subscriber, specified as a Subscriber object handle. You can create the subscriber using rossubscriber.

Timeout for receiving a message, specified as a scalar in seconds.

Output Arguments

collapse all

ROS message, returned as a Message 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 "DataFormat" name-value argument to "struct". For more information, see ROS Message Structures.

Compatibility Considerations

expand all

Behavior change in future release

Extended Capabilities

Introduced in R2019b