Having trouble reading ROS 2 bag with custom messages
Show older comments
I used ros2genmsg to generate the custom message types, but then I run ros2bagreader, it fails. It says that it doesn't recognize one of the message types.
When I run "ros2 msg list" in matlab, the custom message does appear...
Would appreciate any help. Thanks in advance!
Answers (1)
Sakshay
on 13 Mar 2023
Hello Ari,
As per my understanding, after running "ros2genmsg" function, you are not able to view the custom messages that are generated using the "ros2 msg list" command.
ROS 2 custom messages are specified in ROS 2 package folders that contain a folder named "msg". The "msg" folder should contain all the custom message type definitions. Hence, you must have a ROS 2 package that contains the required "msg" file.
After ensuring that your custom message package is correct, you specify the path to the parent folder and call "ros2genmsg" with the specified path. The commands for that would look as below:
folderPath = fullfile(pwd,"<ROS2 Package Name>");
ros2genmsg(folderPath);
After running this command, make sure that the output indicates a successful build. After a successful build, you may run the "ros2 msg list" command to make sure the custom message is generated correctly.
For more information on generating custom messages from ROS2 definitions, you may refer to the following documentation:
1 Comment
Dylan
on 21 May 2026 at 9:24
Unlikely this old post will get more attention, but the issue raised by Ari is definitely still present. The answer above is based on the idea that the message wasn't generated - whereas Ari (and myself) can specify that ros2genmsg is definitely correctly generating the messages. In the same session I can run code to generate a matlab variable of the correct custom message:
ros2msg = ros2message("custom_msgs/TargetOdometry");
disp(ros2msg.MessageType) -> custom_msgs/TargetOdometry
However if I try and read a rosbag with this message it fails. Example below:
% ekf result
ekf = select(reader, "Topic", "/x500/ekf/target_odometry");
ekf = readMessages(ekf);
ekf = [ekf{:}];
ekf_local_coords = zeros([length(ekf), 3]);
ekf_local_coords(:,1) = arrayfun(@(m) m.pose.pose.position.x, ekf);
ekf_local_coords(:,2) = arrayfun(@(m) m.pose.pose.position.y, ekf);
ekf_local_coords(:,3) = arrayfun(@(m) m.pose.pose.position.z, ekf);
Without output:
Warning: The message type "custom_msgs/TargetOdometry" is not a recognized custom message in MATLAB. As a result, messages of the same type belonging to topic "/x500/ekf/target_odometry" get skipped during bag file reading. To resolve this issue, use the 'ros2genmsg' function to add the custom message to MATLAB.
Notably, the issue happens on my Ubuntu 22.04 desktop. Running the same MATLAB script on an M1 Macbook Pro does not seem to have any issues reading the bag file, and I get message data. Quite frustrating, any solutions would be recommended. Last time I ran into this issue it did eventually work, but was unclear if or what I changed to make it happen. The suggested workflow is definitely not working however.
Categories
Find more on ROS Network Access in Simulink in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!