Skip to content

Instantly share code, notes, and snippets.

@rahulbhadani
Last active January 27, 2024 07:31
Show Gist options
  • Save rahulbhadani/f4e5b086860ec5fa9f39807225537d9f to your computer and use it in GitHub Desktop.
Save rahulbhadani/f4e5b086860ec5fa9f39807225537d9f to your computer and use it in GitHub Desktop.
Reading bag files in MATLAB

Reading bag files in MATLAB

MATLAB provides toolbox for reading bag files in a number of ways. For MATLAB Version 2019a and beyond, ROS Toolbox is required for reading bag files. For earlier version upto 2016a, Robotic System Toolbox is required.

I have also written a high-level wrapper for reading and visualizing some widely known topics by a few API calls only. Details are described below:

Using MATLAB Toolbox to read bag files

Start MATLAB by typing matlab in the terminal if you use Linux or double click MATLAB icon if you are using windows.

b = rosbag('2015-12-14-15-59-38.bag');
bagselect = select(b, 'Topic', '/catvehicle/vel'); % topic /catvehicle/vel has tyoe geometry_msgs/Twist
ts = timeseries(bagselect, 'Linear.X'); % We are interested in longitudinal velocity in X-direction

% plot the data
plot(ts.Time, ts.Data);

Using ROSBagReader

I have written high-level wrapper to decode a lot of topics in a few line or for quick visualization. ROSBagReader saves file as .mat and .csv file format in the same location where original .bag file is located.

Installation and usage

Assume that your working directory is /home/ubuntu. You can type pwd in the terminal to get the working directory.

  1. Clone the GitHub repo
git clone https://github.com/jmscslgroup/ROSBagReader ROSBagReader

In MATLAB, add the ROSBagReader's path as follows:

addpath("/home/ubunut//ROSBagReader");

You can use ROSBagReader as follows:

B = ROSBagReader('2019-09-11-14-59-34.bag');

% Extract Velocity Data
B.extractVelData();

Here, I have implicit assumption that velocity data have type geometry_msgs/Twist. If such is not the case, then use MATLAB's direct rosbag functionalities.

B.extractVelData() will create a .mat file and a .csv file corresponding to each velocity topic in 2019-09-11-14-59-34/ directory. You will be able to read .mat file or .csv file as you do with other kind of data.

A detailed instructions on usage of ROSBagReader can be found out at https://github.com/jmscslgroup/ROSBagReader/blob/master/README.md. You can also type doc ROSBagReader in the MATLAB command prompt to get more information about ROSBagReader if you have already added its path via addpath command.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment