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 1.2941 seconds.
Initializing ROS master on
Initializing global node /matlab_global_node_62084 with NodeURI http://bat1002911glnxa64:44905/

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

sub = rossubscriber('/scan');

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

msg2 = receive(sub,10)
msg2 = 
  ROS LaserScan message with properties:

       MessageType: 'sensor_msgs/LaserScan'
            Header: [1x1 Header]
          AngleMin: -0.5216
          AngleMax: 0.5243
    AngleIncrement: 0.0016
     TimeIncrement: 0
          ScanTime: 0.0330
          RangeMin: 0.4500
          RangeMax: 10
            Ranges: [640x1 single]
       Intensities: [0x1 single]

  Use showdetails to show the contents of the message

Shutdown the timers used by sample network.


Shut down ROS network.

Shutting down global node /matlab_global_node_62084 with NodeURI http://bat1002911glnxa64:44905/
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.

Introduced in R2019b