Main Content

readMessages

R2026b

Read messages from ros2bagreader object

    Description

    msgs = readMessages(bag) returns data from all messages in the ros2bagreader object bag. The function returns the messages as a cell array of structures.

    example

    msgs = readMessages(bag,rows) returns data from the messages in the rows specified by rows.Valid row indices range from 1 to bag.NumMessages. readMessages begins reading as soon as the requested index range has been indexed in the background, without waiting for the entire message list to be built. If the requested range extends beyond the currently indexed portion, MATLAB® waits until that range becomes available.

    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.

    Rows of the ros2bagreader object, specified as an n-element vector. n is the number of rows to retrieve messages from. Each entry in the vector corresponds to a numbered message in the bag. The range of the rows is [1 bag.NumMessages].

    Output Arguments

    collapse all

    ROS 2 message data, returned as a cell array of structures.

    Version History

    Introduced in R2021a

    expand all