How can I load, read and save ros ".msg" file by Matlab?
Show older comments
I am trying to load those Autoware code in to Matlab and create a excel files. Autoware is on ROS. ROS has some massage files. I am trying to load those files by MATLAB and get the output as execel files. For example, I have a ROS .msg file called "AccelCmd.msg". Inside of this message I have something as :
Header header
int32 accel
I am able to open it by duble cleck this from "Current folder" window. Or I can right cleck and choose open it as txt. However, that is not what I want. I want load them and save them as a csv or xsml files. Anyone can help me with it?
Thank you.
Answers (1)
Cam Salzberger
on 21 Jan 2019
Edited: Cam Salzberger
on 22 Jan 2019
2 votes
Hello David,
If you are simply looking to import the text from ROS message files, then possibly write it back out into a different file format, there are many ways of pulling text into MATLAB. I'd suggest using the Import Tool as a good first step, as you can use it to generate code for later use in automatically importing text. textscan is also useful if you just want to pull all the text in, and parse it to ignore comment and blank lines. If none of the files have comments, you could check out readtable.
Now that is all about how to get the text data into MATLAB. However, most of the time people are looking to do something with ROS MSG files, they are looking to create ROS message objects in MATLAB, and send and receive that type of ROS message from MATLAB to an external network. If you would like to use MATLAB to access data in these messages (whether the messages are in rosbags or received over the ROS network), you can follow the ROS Custom Message workflow to add support for these messages to MATLAB.
Hope that helps!
-Cam
7 Comments
Sui
on 21 Jan 2019
Cam Salzberger
on 22 Jan 2019
Hello David,
I'm afraid I don't understand what you are asking for at all. Saying that the "message files you have is not data just data" is rather confusing. Can you maybe give me an example of the input you have, and the output you would like to see?
For instance, you might have:
Output: An Excel file that shows the fields of the message, the datatype of those fields, and any constant or default values they have. For example:

Alternatively, you might desire:
Input: Several custom message definitions like this one, and a rosbag that contains messages in those formats.
Output: An Excel file that shows the value of the fields in the messages on one topic in the rosbag, with the timestamp of that message as the header. For example:

That would really help me understand what you are asking for.
-Cam
Cam Salzberger
on 22 Jan 2019
Edited: Cam Salzberger
on 22 Jan 2019
Hey David,
Thanks for clarifying that. I understand what you are looking for now.
For your simple "AdjustXY" message, you can easily do that in just a few lines:
messageName = 'AdjustXY';
% Read the information about the message and store it in a table
tableMessage = readtable([messageName '.msg'], 'FileType', 'text', 'ReadVariableNames', false);
% Prepare the first Excel column with the name of the message
toAppend = repmat({messageName}, size(tableMessage, 1), 1);
tableMessage = [toAppend tableMessage];
% Output the message information into an Excel file
writetable(tableMessage, [messageName '.xlsx'], 'WriteVariableNames', false)
As an aside, I've edited my original answer to recommend readtable over dlmread. I had forgotten that dlmread was just for numeric data.
This is simple because the message only had the two columns, data type and field name. If the message were more complicated, like this for example:
Header header
int32 x
int32 y
uint8 SOMECONSTANT=1
uint8[] some_array
The same code used on this message definition will result in an Excel file looking like this:

And if there are any comments in the file, that will really throw off the automatic way readtable interprets columns. So if you are concerned about parsing all messages, or messages in a general case, I'd recommend playing around with more low-level text-reading functions.
textscan should probably work fine, if you read everything in as a long string, and then post-process on the line to extract what you need. You may want to use strsplit on the line to break it up based on the locations of spaces and other delimiters. However, this is getting to be a hefty project, so you will need to tailor it to your specific needs.
Best of luck to you with this!
-Cam
Sui
on 22 Jan 2019
Sui
on 24 Jan 2019
Categories
Find more on Parallel for-Loops (parfor) 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!




