Why is the ROS subscriber callback in MATLAB not triggered when messages are published from an external ROS node (not in MATLAB)?

113 views (last 30 days)
Reproduction steps:
ROS Master:
In Linux terminal,
$ roscore
In a new Linux terminal,
$ rostopic pub -r 2 /chatter std_msgs/String {}
Node = rosmatlab.node('Test','http://localhost:11311');
Sub = Node.addSubscriber('/chatter','std_msgs/String',10);
In a new file “test_fun.m”:
function test_fun(msg)
disp('Message received!')
With the above setup, no messages are received in MATLAB.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 24 Feb 2014
This issue arises because the subscriber cannot resolve the URI of the publisher. When a ROS node advertises a topic, it provides a hostname:port combination (a URI) that other nodes use to establish contact when they want to subscribe to that topic. It is important that the hostname the publishing node provides can be used by all other nodes to contact it. The ROS client libraries use the name reported by the machine as hostname. This is the name that is returned by the Linux command “hostname”. If a machine reports a hostname that is not addressable by other machines, you need to set either the ROS_IP or ROS_HOSTNAME environment variables. (Reference: http://wiki.ros.org/ROS/NetworkSetup)
ROS_IP and ROS_HOSTNAME are optional environment variables that set the declared network address of a ROS Node or tool. They are mutually exclusive; if both are set, ROS_HOSTNAME will take precedence. Use ROS_IP if you are specifying an IP address and ROS_HOSTNAME if you are specifying a name. (Reference: http://wiki.ros.org/ROS/EnvironmentVariables)
The environment variable ROS_MASTER_URI is required when the ROS core is not running on localhost. ROS_MASTER_URI  are used by ROS nodes to locate the master.
TO RESOLVE THIS ISSUE, ALWAYS SPECIFY ROS_IP BEFORE STARTING A NODE ON LINUX. Use the IP address of the network interface on which the node is communicating with the ROS network.
To ensure the correct behavior of the MATLAB ROS support package, it is recommended that you always specify ROS_IP and ROS_MASTER_URI explicitly.  
For example, if the ROS core is running on a machine with IP: and MATLAB is running on a machine with IP:, then we should set up the environment variables as follows (please replace the IP addresses below with the IP addresses of your network setup):
In Linux terminal:
$ export ROS_IP=
$ export ROS_MASTER_URI=
$ start a roscore, publisher or subscriber
To make the above change permanent, put the above two export commands in the “.bashrc” file, as follows,
$ cd ~
$ gedit .bashrc
At the end of the file “.bashrc”, add the two export statements.
export ROS_IP=
Restart Linux terminal.
  1 Comment
olaf just
olaf just on 10 Mar 2017
Thank you very much. This solves my problem with the youbot and rosserial after a whole week of trying. The export ROS_IP=<IP of my MatlabPC> was missing in the .bashrc. Now I am able to get ADC-Data from my Arduino and use them in Matlab.

Sign in to comment.

More Answers (3)

Vincent Talon
Vincent Talon on 2 Jul 2017
Edited: Walter Roberson on 5 Jul 2017
I use Matlab 2017a with Windows10, I remove all firewall
on my turtelbot the .batchrc have :
export ROS_IP=
In Matlab, I add :
I'm well able to read all turtlebot topic with echo on Bash terminal on Windows10
I'm well able to order to trutrlebot to move (with matlab)
velocity = -0.1; % meters per second
robot = rospublisher('/mobile_base/commands/velocity')
velmsg = rosmessage(robot)
velmsg.Linear.X = velocity;
for i=1:50000
But when I try to creat a subscriber
odom = rossubscriber('/odom')
odomdata = receive(odom,3);
With 2nd line, I have all time the same error message
Error using robotics.ros.Subscriber/receive (line 325)
The function did not receive any data and timed out.
to synthesis
  1. I'm able to display all rostopic --> rostopic list
  2. I'm able to send order of moving to turtlebot with Matlab
  3. I'm not able to ready any ROS topic
What I can test ?
thank you in advance
Chang Liu
Chang Liu on 14 Jul 2017
Refer to the link given by @Stefan below. It does work! I just tested it.
The step 1 and 2 given in the link are important.

Sign in to comment.

Stefan on 13 Jun 2017
Edited: Stefanie Schwarz on 2 Aug 2021

Cameron LaMack
Cameron LaMack on 15 Dec 2022
On the off chance someone has found this page while trying to control the Baxter Robot from Rethink Robotics, I cannot recommend this fileshare enough: https://www.mathworks.com/matlabcentral/fileexchange/63564-baxter-communicator-class?s_tid=FX_rc2_behav


Find more on Network Connection and Exploration in Help Center and File Exchange



No release entered yet.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!