Main Content

select

R2026b

Select subset of messages in ros2bagreader

    Description

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

    This function creates a copy of the ros2bagreader object or returns a new ros2bagreader object that contains the specified message selection.

    example

    bagsel = select(bag,Name=Value) provides additional options specified by one or more name-value arguments. For example, Topic="/scan" selects a subset of the messages filtered by the topic /scan.

    The select function does not wait for the full message list when filtering by Topic or TopicType. When filtering by Time, the function waits until message indexing completes for the queried time range before returning results.

    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("ros2_netwrk_bag.zip");
    folderPath = fullfile(pwd,"ros2_netwrk_bag");

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

    bagReader = ros2bagreader(folderPath);

    Get information about the contents of the ros2bagreader object.

    baginfo = ros2("bag","info",folderPath)
    baginfo = struct with fields:
                     Path: 'C:\Users\user\OneDrive - MathWorks\Documents\MATLAB\ExampleManager\user.Bdoc26b\ros-ex96596996\ros2_netwrk_bag'
          FileInformation: "ros2_netwrk_bag.db3"
                  Version: '1'
                StorageId: 'sqlite3'
                 Duration: 207.9020
                    Start: [1×1 struct]
                      End: [1×1 struct]
                     Size: 16839538
                 Messages: 166867
                    Types: [4×1 struct]
                   Topics: [4×1 struct]
        CompressionFormat: 'none'
          CompressionMode: 'none'
    
    

    Get all the messages in the ros2bagreader object.

    msgs = readMessages(bagReader);

    Select a subset of the messages, filtered by topic.

    bagSel = select(bagReader,"Topic","/odom")
    bagSel = 
      ros2bagreader with properties:
    
               FilePath: 'C:\Users\user\OneDrive - MathWorks\Documents\MATLAB\ExampleManager\user.Bdoc26b\ros-ex96596996\ros2_netwrk_bag'
          AvailableBags: "ros2_netwrk_bag.db3"
              StartTime: 1.6020e+09
                EndTime: <preparing>
        AvailableTopics: [1×3 table]
        AvailableFrames: {0×1 cell}
            MessageList: <preparing>
            NumMessages: 5275
    
    

    Get the messages in the selection.

    msgsFiltered = readMessages(bagSel);

    Input Arguments

    collapse all

    Messages in the ros2bagreader object, specified as a ros2bagreader object.

    Name-Value Arguments

    collapse all

    Specify optional pairs of arguments as Name1=Value1,...,NameN=ValueN, where Name is the argument name and Value is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

    Before R2021a, use commas to separate each name and value, and enclose Name in quotes.

    Example: select(bag,Topic="/scan") selects a subset of the messages, filtered by the topic /scan.

    ROS 2 message type, specified as a string scalar, character vector, cell array of string scalars, or cell array of character vectors. Multiple message types can be specified with a cell array.

    Example: select(bag,MessageType={"sensor_msgs/CameraInfo","sensor_msgs/LaserScan"})

    Data Types: char | string | cell

    Start and end times of the ROS 2 bag selection, specified as an n-by-2 vector.

    Example: select(bag,Time=[bag.MessageList(1,1).Time,bag.MessageList(2,1).Time])

    Data Types: double

    ROS 2 topic name, specified as a string scalar, character vector, cell array of string scalars, or cell array of character vectors. Multiple topic names can be specified with a cell array.

    Example: select(bag,Topic={"/scan","/clock"})

    Data Types: char | string | cell

    Output Arguments

    collapse all

    Copy or subset of ROS 2 bag messages, returned as a ros2bagreader object.

    Version History

    Introduced in R2021a

    expand all