Main Content

rosbagreader

Access rosbag log file information

    Description

    The rosbagreader 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 time series of the message properties.

    Creation

    Description

    example

    bagreader = rosbagreader(filepath) creates an indexable rosbagreader object, bagreader, that contains all the messages from the rosbag log file at the input path filepath. The filepath input argument sets the FilePath property. To access the data, you can call readMessages or timeseries to extract relevant data.

    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.

    Data Types: table

    This property is read-only.

    List of available coordinate frames, specified 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

    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

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

    Examples

    collapse all

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

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

    bagMsgs = rosbagreader("ros_multi_topics.bag")
    bagMsgs = 
      rosbagreader with properties:
    
               FilePath: '/mathworks/devel/bat/Bdoc21b/build/matlab/toolbox/robotics/robotexamples/ros/data/bags/ros_multi_topics.bag'
              StartTime: 201.3400
                EndTime: 321.3400
            NumMessages: 36963
        AvailableTopics: [4x3 table]
        AvailableFrames: {0x1 cell}
            MessageList: [36963x4 table]
    
    

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

    bagMsgs2 = select(bagMsgs,...
        Time=[bagMsgs.StartTime bagMsgs.StartTime + 1],...
        Topic='/odom')
    bagMsgs2 = 
      rosbagreader with properties:
    
               FilePath: '/mathworks/devel/bat/Bdoc21b/build/matlab/toolbox/robotics/robotexamples/ros/data/bags/ros_multi_topics.bag'
              StartTime: 201.3400
                EndTime: 202.3200
            NumMessages: 99
        AvailableTopics: [1x3 table]
        AvailableFrames: {0x1 cell}
            MessageList: [99x4 table]
    
    

    Retrieve the messages in the selection as a cell array.

    msgs = readMessages(bagMsgs2)
    msgs=99×1 cell array
        {1x1 Odometry}
        {1x1 Odometry}
        {1x1 Odometry}
        {1x1 Odometry}
        {1x1 Odometry}
        {1x1 Odometry}
        {1x1 Odometry}
        {1x1 Odometry}
        {1x1 Odometry}
        {1x1 Odometry}
        {1x1 Odometry}
        {1x1 Odometry}
        {1x1 Odometry}
        {1x1 Odometry}
        {1x1 Odometry}
        {1x1 Odometry}
          ⋮
    
    

    Return certain message properties as a time series.

    ts = timeseries(bagMsgs2,...
        'Pose.Pose.Position.X', ...
        'Twist.Twist.Angular.Y')
      timeseries
    
      Timeseries contains duplicate times.
    
      Common Properties:
                Name: '/odom Properties'
                Time: [99x1 double]
            TimeInfo: tsdata.timemetadata
                Data: [99x2 double]
            DataInfo: tsdata.datametadata
    

    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.

    bagMsgs = rosbagreader("ros_turtlesim.bag")
    bagMsgs = 
      rosbagreader with properties:
    
               FilePath: '/tmp/Bdoc21b_1757077_258668/tpe8a32ce2/ros-ex81142742/ros_turtlesim.bag'
              StartTime: 1.5040e+09
                EndTime: 1.5040e+09
            NumMessages: 6089
        AvailableTopics: [6x3 table]
        AvailableFrames: {2x1 cell}
            MessageList: [6089x4 table]
    
    

    Get a list of available frames.

    frames = bagMsgs.AvailableFrames
    frames = 2x1 cell
        {'turtle1'}
        {'world'  }
    
    

    Get the latest transformation between two coordinate frames.

    tf = getTransform(bagMsgs,'world',frames{1})
    tf = 
      ROS TransformStamped message with properties:
    
         MessageType: 'geometry_msgs/TransformStamped'
              Header: [1x1 Header]
           Transform: [1x1 Transform]
        ChildFrameId: 'turtle1'
    
      Use showdetails to show the contents of the message
    
    

    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(bagMsgs.StartTime + 1);
    if (canTransform(bagMsgs,'world',frames{1},tfTime))
        tf2 = getTransform(bagMsgs,'world',frames{1},tfTime);
    end
    Introduced in R2021b