MATLAB Examples

Get Started with ROS



Robot Operating System (ROS) is a communication interface that enables different parts of a robot system to discover, send, and receive data. MATLAB® support for ROS is a library of functions that allows you to exchange data with ROS-enabled physical robots, or robot simulators such as Gazebo®.

This example introduces how to:

  • Set up ROS within MATLAB
  • Get information about capabilities in a ROS network
  • Get information about ROS messages

ROS Terminology

  • A ROS network comprises different parts of a robot system (such as a planner or a camera interface) that communicate over ROS. The network can be distributed over several machines.
  • A ROS master coordinates the different parts of a ROS network. It is identified by a Master URI (Uniform Resource Identifier) that specifies the hostname or IP address of the machine where the master is running.
  • A ROS node is an entity that contains a collection of related ROS capabilities (such as publishers, subscribers and services). A ROS network can have many ROS nodes.
  • Publishers, subscribers, and services are different kinds of ROS entities that process data. They exchange data using messages.
  • A publisher sends messages to a specific topic (such as "odometry"), and subscribers to that topic receive those messages. There can be multiple publishers and subscribers associated with a single topic.

For more information, see docid:robotics_gs.bumviaw-1 and the Concepts section on the ROS website.

Initialize ROS Network

  • Use rosinit to initialize ROS. By default, rosinit creates a ROS master in MATLAB and starts a "global node" that is connected to the master. The "global node" is automatically used by other ROS functions.
Initializing ROS master on http://bat7842maci:64410/.
Initializing global node /matlab_global_node_53019 with NodeURI http://bat7842maci:64414/
  • Use rosnode list to see all nodes in the ROS network. Note that the only available node is the global node created by rosinit.
rosnode list
  • Use exampleHelperROSCreateSampleNetwork to populate the ROS network with three additional nodes and sample publishers and subscribers.
  • Use rosnode list again, and observe that there are three new nodes (node_1, node_2 and node_3).
rosnode list

A visual representation of the current state of the ROS network is shown below. Use it as a reference when you explore this sample network in the remainder of the example.

The MATLAB global node is disconnected since it currently does not have any publishers, subscribers or services.


  • Use rostopic list to see available topics in the ROS network. Observe that there are three active topics: /pose, /rosout, and /scan. rosout is a default logging topic that is always present in the ROS network. The other two topics were created as part of the sample network.
rostopic list
rostopic info /pose
Type: geometry_msgs/Twist
* /node_1 (http://bat7842maci:64422/)
* /node_2 (http://bat7842maci:64428/)
  • Use rosnode info to get information about a specific node. The command below show that node_1 publishes to /pose and /rosout topics, and subscribes to the /scan topic.
rosnode info /node_1
Node: [/node_1]
URI: [http://bat7842maci:64422/]
Publications (2 Active Topics): 
 * /pose
 * /rosout
Subscriptions (1 Active Topics): 
 * /scan
Services (0 Active): 


ROS Services provide a mechanism for "procedure calls" across the ROS network. A service client sends a request message to a service server, which processes the information in the request and returns with a response message (see docid:robotics_examples.example-ROSServicesExample).

  • Use rosservice list to see all available service servers in the ROS network. The command below shows that two services (/add and /reply) are available.
rosservice list
  • Use rosservice info to get information about a specific service.
rosservice info /add
Node: /node_3
URI: rosrpc://bat7842maci:64435/
Type: roscpp_tutorials/TwoInts
Args: A B


Publishers, subscribers, and services use ROS messages to exchange information. Each ROS message has an associated message type that defines the datatypes and layout of information in that message (See docid:robotics_examples.example-ROSMessagesExample).

  • Use rostopic type to see the message type used by a topic. The command below shows that the /pose topic uses messages of type geometry_msgs/Twist.
rostopic type /pose
  • Use rosmsg show to view the properties of a message type. The geometry_msgs/Twist message type has two properties, Linear and Angular. Each property is a message of type geometry_msgs/Vector3, which in turn has three properties of type double.
rosmsg show geometry_msgs/Twist
% This expresses velocity in free space broken into its Linear and Angular parts.
Vector3  Linear
Vector3  Angular

rosmsg show geometry_msgs/Vector3
% This represents a vector in free space. 

double X
double Y
double Z
  • Use rosmsg list to see the full list of message types available in MATLAB.
rosmsg list

Shut Down ROS Network

  • Use exampleHelperROSShutDownSampleNetwork to remove the sample nodes, publishers, and subscribers from the ROS network. This command is only needed if the sample network was created earlier using exampleHelperROSStartSampleNetwork.
  • Use rosshutdown to shut down the ROS network in MATLAB. This shuts down the ROS master that was started by rosinit and deletes the global node. It is recommended to use rosshutdown once you are done working with the ROS network.
Shutting down global node /matlab_global_node_53019 with NodeURI http://bat7842maci:64414/
Shutting down ROS master on http://bat7842maci:64410/.

Next Steps