How to access ROS message data from ROS 2 bag file containing custom message without using ros2genmsg in MATLAB R2024a?
34 views (last 30 days)
Show older comments
MathWorks Support Team
on 16 Jul 2024
Answered: MathWorks Support Team
on 16 Jul 2024
I have a rosbag file in the form of a db3 file. I tried using ros2bagreader, however, since there are custom messages involved, it would fail to read and throw error. I do not have access to the original message template and hence cannot use the ros2genmsg to resolve this issue. I wanted to know if it is possible to read the data from the database file despite the errors.
Accepted Answer
MathWorks Support Team
on 16 Jul 2024
Since this is an SQLite database file, we can leverage the Database Toolbox functionality to handle SQLite objects. You can access the data of all the messages and topics with below code:
dbfile = "rosbagFile.db3"; % Filename of the database file
conn = sqlite(dbfile, 'readonly');
tables = sqlfind(conn,'') % Finds all tables in the database
% Assuming that the above database contains two tables: topics and messages
topic = fetch(conn,'SELECT * FROM topics') % Fetches topics table from the database which also contains their ids
msg = fetch(conn,'SELECT * FROM messages') % Fetches messages table from teh database where all data along with their topic ids are recorded
close(conn); % closing the connection
First, you will have to connect with the SQLite database using 'sqlite' command. Next, we find the tables that are present in the database with the 'sqlfind' command. You can then extract the tables using the 'fetch' command and then go on to extract the column needed.
Without Database Toolbox to execute above commands, you would have to rely on SQLite installed externally and using MATLAB to parse system commands to open and execute SQL commands, following similar workflow as above.
After installing SQLite, execute the below code snippet to obtain the tables available in the db3 file:
sqlitePath = 'path_to_sqlite_executable';
dbfile = 'yourfile.db3';
query = '.table';
% Construct the command to run SQLite
command = sprintf('"%s" "%s" "%s"', sqlitePath, dbfile, query);
% Execute the command and capture the output
[status, result] = system(command);
After obtaining the table name from above, you can then fetch the table and the data from that table with the following command:
query = 'SELECT * FROM tableName';
% Construct the command to run SQLite
command = sprintf('"%s" "%s" "%s"', sqlitePath, dbfile, query);
% Execute the command and capture the output
[status, result] = system(command);
The above will give access to the data in the table in MATLAB.
0 Comments
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!