Setup ROS subscriber in Matlab

5 views (last 30 days)
Tristan Sinnecker
Tristan Sinnecker on 4 Feb 2022
Answered: Cam Salzberger on 7 Feb 2022
Dear all,
I am trying to subscribe to a ROS /topic in Matlab and I am running out of ideas, what could cause my errors. On my remote machine with Ubuntu 20.4 and ROS Noetic, I can confirm, that the topic is publishing (rostopic echo /topicname). Trying to use the same command in Matlab does not give any output.
In Matlab I run the following code:
pyenv('Version', '2.7')
rosIP = '';
rosHOSTNAME = '';
setenv('ROS_IP', rosIP);
Shutdown existing ROS server and connect to ROS server on Tinkerboard
Display all available ros nodes and its topics
rosnode list
rostopic list
temperatureSub = rossubscriber('/SOLO1_Temperature', 'std_msgs/Int32')
temperatureMsg = receive(temperatureSub,5)
I get the following error message:
Error using ros.Subscriber/receive (line 456)
The function did not receive any data and timed out.
Do I have to define a callback function first? Or could the error occur because of ROS Noetic? Afaik Matlab only supports ROS Melodic.
Thank you very much!

Answers (1)

Cam Salzberger
Cam Salzberger on 7 Feb 2022
Hello Tristan,
If the "receive" call is timing out, that is because it did not receive any data within the timeout period. First, check the obvious:
  • Are messages being sent on that specified topic continuously?
  • Are the messages being sent frequently enough that the timeout period would cover at least two messages?
  • Are you able to see messages being sent on that topic by running "rostopic echo" in another terminal on the machine sending the messages?
If it seems like messages are being published correctly, then the issue is the connection between the machines. First check that the topic is visible to MATLAB by doing "rostopic list" (before creating any subscribers or publishers in MATLAB). If you can see the topic with introspection, but not receive any messages on it, the issue is usually with the environment (usually ROS_IP) on the machine sending the messages. See here for a detailed answer.


Find more on Get Started with ROS Toolbox in Help Center and File Exchange




Community Treasure Hunt

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

Start Hunting!