Main Content

readMessages

Read messages from rosbag

Since R2019b

Description

example

msgs = readMessages(bag) returns data from all the messages in the BagSelection or rosbagreader object bag. The messages are returned in a cell array of messages. To get a BagSelection object, use rosbag.

example

msgs = readMessages(bag,rows) returns data from messages in the rows specified by rows. The range of the rows is [1, bag.NumMessages].

example

msgs = readMessages(___,"DataFormat",Format) returns data as a cell array of structures or cell array of message objects using either set of the previous input arguments. Specify Format as either "struct" or "object".

Using structures can be significantly faster than using message objects, and custom message data can be read directly without loading message definitions using rosgenmsg.

Note

In a future release, ROS Toolbox will use message structures instead of objects for ROS messages.

To use message structures now, set the "DataFormat" name-value argument to "struct". For more information, see ROS Message Structures.

Examples

collapse all

Read rosbag and filter by topic and time.

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

Return all messages as a cell array.

allMsgs = readMessages(bagselect2);

Return the first ten messages as a cell array.

firstMsgs = readMessages(bagselect2,1:10);

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)

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("ex_multiple_topics.bag")
bagMsgs = 
  rosbagreader with properties:

           FilePath: '/tmp/Bdoc24a_2528353_1173766/tp199fe096/ros-ex26633229/ex_multiple_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: '/tmp/Bdoc24a_2528353_1173766/tp199fe096/ros-ex26633229/ex_multiple_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

Input Arguments

collapse all

Index of the messages in the rosbag, specified as a BagSelection or rosbagreader object.

Rows of the BagSelection or rosbagreader object, specified as an n-element vector, where 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.NumMessage].

Output Arguments

collapse all

ROS message data, returned as an object, cell array of message objects, or cell array of structures. Data comes from either the BagSelection object created using rosbag or the rosbagreader object.

You must specify "DataFormat","struct" in the function to get messages as a cell array of structures. Using structures can be significantly faster than using message objects, and custom message data can be read directly without loading message definitions using rosgenmsg.

Version History

Introduced in R2019b

expand all