MATLAB: Unable to access ROSbag topics

robotic system toolboxRobotics System Toolboxrosrosbag

I have a bag file and would like to work with it in MATLAB.
I can view the messages for one of my topics using:
>> bag = rosbag('HMB_1.bag');
>> bagselect_c = select(bag, 'Topic', '/center_camera/image_color/compressed');
>> msgs_c = readMessages(bagselect_c);
when I try to do the same for another topic, I get an error:
>> bagselect= select(bag, 'Topic', '/vehicle/steering_report');
>> msgstemp = readMessages(bagselect);
Error using robotics.ros.BagSelection/deserializeMessages (line 445)
Java exception occurred:
org.ros.exception.RosMessageRuntimeException: java.lang.ClassNotFoundException: dbw_mkz_msgs.SteeringReport
at org.ros.internal.message.definition.MessageDefinitionReflectionProvider.get(MessageDefinitionReflectionProvider.java:66)
at org.ros.internal.message.DefaultMessageFactory.newFromType(DefaultMessageFactory.java:42)
at org.ros.internal.message.DefaultMessageDeserializer.deserialize(DefaultMessageDeserializer.java:42)
Caused by: java.lang.ClassNotFoundException: dbw_mkz_msgs.SteeringReport
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at org.ros.internal.message.definition.MessageDefinitionReflectionProvider.get(MessageDefinitionReflectionProvider.java:62)
... 2 more
Error in robotics.ros.BagSelection/readMessages (line 195)
msgs = obj.deserializeMessages(obj.MessageList, rows);
I'm not sure how to troubleshoot.

Best Answer

This error is a result of the rosbag containing custom message types (e.g. 'dbw_mkz_msgs.SteeringReport') that are not supported out of the box.
However,there is a separate support package which can help with that. Please refer to the following link to access the support package:
Note:This support package will make custom message types available in MATLAB.
  • For the next steps, please note that you will need the "rt_msgs ROS package" (since it contains the message definition for the custom message). This package is not publicly available, but the full rt_msgs package can be obtained from the machine which recorded the bag file.
  • Create a new folder in a writable location, e.g. "custom_messages"
  • Copy the "rt_msgs" package folder into "custom_messages"
  • Execute "rosgenmsg('custom_messages')". This will generate all necessary files that MATLAB needs to handle dbw_mkz_msgs.SteeringReport.
  • Once it finishes successfully, "rosgenmsg" will display three steps (modify javaclasspath, change MATLAB path, restart MATLAB). Please follow these steps.
  • After restarting MATLAB, you should be able to read the rosbag and extract the messages.
Hope this helps!