Main Content

ros2bag

Open and parse ros2bag log file

    Description

    The ros2bag object contains all the messages from the ROS 2 bag log file.

    Creation

    Description

    example

    bag = ros2bag(folderpath) creates a ros2bag object, bag, that contains all the messages from the ROS 2 bag log file located at path folderPath. The folderpath input sets the value of the FilePath property.

    ROS 2 bag files are used for storing ROS 2 message data. They are used primarily to log messages transmitted over a ROS 2 network. You can use these bag files for offline analysis, visualization, and storage.

    Note

    If the ROS 2 bag log file contains custom messages, create custom messages for MATLAB using ros2genmsg function before creating the ros2bag object.

    Properties

    expand all

    This property is read-only.

    Path to the ROS 2 bag files, specified as a character vector.

    Data Types: char

    This property is read-only.

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

    Data Types: uint64

    This property is read-only.

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

    Data Types: uint64

    This property is read-only.

    Number of messages, specified as a scalar.

    Data Types: double

    This property is read-only.

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

    Data Types: table

    This property is read-only.

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

    Data Types: table

    Object Functions

    readMessagesRead messages from ros2bag object
    selectSelect subset of messages in ros2bag

    Examples

    collapse all

    Extract the zip file that contains the ROS 2 bag log file and specify the full path to the log folder.

    unzip('alltopics.zip');
    folderPath = fullfile(pwd,'alltopics');

    Create a ros2bag object that contains all the messages in the log file.

    bag = ros2bag(folderPath);

    Get information on the contents of the ros2bag object.

    bag2info = ros2("bag","info",folderPath)
    bag2info = struct with fields:
             Path: 'C:\Users\SHIVARAD\OneDrive - MathWorks\Documents\MATLAB\Examples\ros-ex96596996\alltopics\alltopics.db3'
          Version: '1'
        StorageId: 'sqlite3'
         Duration: 102.396644003
            Start: [1×1 struct]
              End: [1×1 struct]
             Size: 4965433
         Messages: 36503
            Types: [5×1 struct]
           Topics: [5×1 struct]
    
    

    Get all the messages in the ros2bag object.

    msgs = readMessages(bag);

    Select a subset of the messages, filtered by topic.

    bagSel = select(bag,"Topic","/scan");

    Get the messages in the selection.

    msgsFiltered = readMessages(bagSel);

    See Also

    Functions

    Introduced in R2021a