Exporting ROS Bag Files to Matlab, v2.0

A while ago I wrote a post about bag2csv, which is a tool I wrote to import data from ROS bags to Matlab workspaces. This tool first converted the bag data to CSV files, and then read the CSV files into Matlab. We’ve discovered a lot of shortcomings to this approach since that post was written, and as a result, have developed a different tool for importing bag data into Matlab. The two largest issues we encountered were correctly putting ROS messages with arbitrary string fields into a CSV file and dealing with complex ROS messages that contained things like variable arrays of other ROS messages. Putting these types of data into CSV files is hard, because arbitrary strings may contain sequences that break the CSV parser, and variable length arrays do not mesh well with CSV’s tabular format.

I wrote a replacement tool for bag2csv called bag2matlab that uses ROS’s Python bag manipulation modules to directly read the bag data into a Matlab workspace. This tool is not only addresses the problems with bag2csv, but is much simpler. Users can call the bagReader.m Matlab function from within their Matlab environment, and it will seamlessly read all of the data on a given topic from the requested bag file. The data is returned as a table, where each column in the table corresponds to a field in the requested topic. In the case where the topic contains nested ROS messages, the tool flattens these structures, so every field in every subtopic has a column in the output table.

Using this tool is is very simple. From within Matlab, users can simply call it using syntax like:

msg_data = bagReader('input.bag', 'topic_name');

Our university Matlab license does not include the Robotics Toolbox, so I do not depend on anything in that toolbox in this project. The only dependencies should be a ROS installation and Matlab. I tested this with ROS Kinetic Kame and Matlab R2015b on Ubuntu 16.04.

I’ve posted the code to GitHub here, and I hope others will find it useful.

Leave a Comment