Main Content

BagSelection

Object for storing rosbag selection

Since R2019b

Description

The BagSelection object is an index of the messages within a rosbag. You can use it to extract message data from a rosbag, select messages based on specific criteria, or create a timeseries of the message properties.

Use rosbag to load a rosbag and create the BagSelection object.

Use select to filter the rosbag by criteria such as time and topic.

Creation

Description

bag = rosbag(filename) creates an indexable BagSelection object, bag, that contains all the message indexes from the rosbag at the input path, filename. To access the data, you can call readMessages or timeseries to extract relevant data.

See rosbag for other syntaxes.

example

bagsel = select(bag) returns an object, bagsel, that contains all the messages in the BagSelection object, bag.

This function does not change the contents of the original BagSelection object. The return object, bagsel, is a new object that contains the specified message selection.

See select for other syntaxes and to filter by criteria such as time and topic.

Properties

expand all

This property is read-only.

Absolute path to the rosbag file, specified as a character vector.

Data Types: char

This property is read-only.

Timestamp of the first message in the selection, specified as a scalar in seconds.

Data Types: double

This property is read-only.

Timestamp of the last message in the selection, specified as a scalar in seconds.

Data Types: double

This property is read-only.

Number of messages in the selection, specified as a scalar. When you first load a rosbag, this property contains the number of messages in the rosbag. Once you select a subset of messages with select, the property shows the number of messages in this subset.

Data Types: double

This property is read-only.

Table of topics in the selection, specified as a table. Each row in the table lists one topic, the number of messages for this topic, the message type, and the definition of the type. For example:

             NumMessages       MessageType                              MessageDefinition                       
             ___________    _________________    _______________________________________________________________

    /odom    99             nav_msgs/Odometry    '# This represents an estimate of a position and velocity in …'

Data Types: table

This property is read-only.

List of available coordinate frames, returned as a cell array of character vectors. Use canTransform to check whether specific transformations between frames are available, or getTransform to query a transformation.

Data Types: cell array

This property is read-only.

List of messages in the selection, specified as a table. Each row in the table lists one message.

Data Types: table

Object Functions

canTransformVerify if transformation is available
getTransformRetrieve transformation between two coordinate frames
readMessagesRead messages from rosbag
selectSelect subset of messages in rosbag
timeseriesCreate time series object for selected message properties
timetableCreate timetable for selected message properties in ROS bag file

Examples

collapse all

Load a rosbag log file and parse out specific messages based on the selected criteria.

Create a BagSelection object of all the messages in the rosbag log file.

bagMsgs = rosbag('ex_multiple_topics.bag');

Select a subset of the messages based on their timestamp and topic.

bagMsgs2 = select(bagMsgs,'Time',...
           [bagMsgs.StartTime bagMsgs.StartTime + 1],'Topic','/odom');

Retrieve the messages in the selection as a cell array.

msgs = readMessages(bagMsgs2);

Return certain message properties as a time series.

ts = timeseries(bagMsgs2,'Pose.Pose.Position.X', ...
           'Twist.Twist.Angular.Y');

Retrieve information from the rosbag. Specify the full path to the rosbag if it is not already available on the MATLAB® path.

bagselect = rosbag('ex_multiple_topics.bag');

Select a subset of the messages, filtered by time and topic.

bagselect2 = select(bagselect,'Time',...
    [bagselect.StartTime bagselect.StartTime + 1],'Topic','/odom');

To view information about a rosbag log file, use rosbag info filename, where filename is a rosbag (.bag) file.

rosbag info 'ex_multiple_topics.bag'
Path:     /tmp/Bdoc24a_2528353_1173766/tp199fe096/ros-ex32890909/ex_multiple_topics.bag
Version:  2.0
Duration: 2:00s (120s)
Start:    Dec 31 1969 19:03:21.34 (201.34)
End:      Dec 31 1969 19:05:21.34 (321.34)
Size:     23.6 MB
Messages: 36963
Types:    gazebo_msgs/LinkStates [48c080191eb15c41858319b4d8a609c2]
          nav_msgs/Odometry      [cd5e73d190d741a2f92e81eda573aca7]
          rosgraph_msgs/Clock    [a9c97c1d230cfc112e270351a944ee47]
          sensor_msgs/LaserScan  [90c7ef2dc6895d81024acba2ac42f369]
Topics:   /clock               12001 msgs  : rosgraph_msgs/Clock   
          /gazebo/link_states  11999 msgs  : gazebo_msgs/LinkStates
          /odom                11998 msgs  : nav_msgs/Odometry     
          /scan                  965 msgs  : sensor_msgs/LaserScan 

Get transformations from rosbag (.bag) files by loading the rosbag and checking the available frames. From these frames, use getTransform to query the transformation between two coordinate frames.

Load the rosbag.

bag = rosbag('ros_turtlesim.bag');

Get a list of available frames.

frames = bag.AvailableFrames;

Get the latest transformation between two coordinate frames.

tf = getTransform(bag,'world',frames{1});

Check for a transformation available at a specific time and retrieve the transformation. Use canTransform to check if the transformation is available. Specify the time using rostime.

tfTime = rostime(bag.StartTime + 1);
if (canTransform(bag,'world',frames{1},tfTime))
    tf2 = getTransform(bag,'world',frames{1},tfTime);
end

Load the rosbag.

bag = rosbag('ros_turtlesim.bag');

Select a specific topic.

bSel = select(bag,'Topic','/turtle1/pose');

Read messages as a structure. Specify the DataFormat name-value pair when reading the messages. Inspect the first structure in the returned cell array of structures.

msgStructs = readMessages(bSel,'DataFormat','struct');
msgStructs{1}
ans = struct with fields:
        MessageType: 'turtlesim/Pose'
                  X: 5.5016
                  Y: 6.3965
              Theta: 4.5377
     LinearVelocity: 1
    AngularVelocity: 0

Extract the xy points from the messages and plot the robot trajectory.

Use cellfun to extract all the X and Y fields from the structure. These fields represent the xy positions of the robot during the rosbag recording.

xPoints = cellfun(@(m) double(m.X),msgStructs);
yPoints = cellfun(@(m) double(m.Y),msgStructs);
plot(xPoints,yPoints)

Version History

Introduced in R2019b