How can I load, read and save ros ".msg" file by Matlab?

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
Cam Salzberger on 21 Jan 2019
Edited: Cam Salzberger on 22 Jan 2019
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

Hi Cam
Thank you for your time. That does help. However, I still do nopt dunerstand. Yes, I can open those msg files in MATLAB as txt. However, I am not sure how can I load thos messages. For example, I am able to use "load" to load data as mat files or csv. But I tried same thing. Those does not work, It shows error not ASCII things. As i said all message files I have is not data just data but most of them are something like text. Also, I tried "rosinit". Not work as I expect. I do not thing those. Will you please give me more info and idea. Thank you.
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:
Input: A text file that contains a message definition. For example.
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:
Excel_file_showing_msg_definition.png
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:
Excel_file_showing_message_data.png
That would really help me understand what you are asking for.
-Cam
Sui
Sui on 22 Jan 2019
Edited: Sui on 22 Jan 2019
Hi Cam
Thank you so much for your time and sorry I did not clear this. For example, I have a message file as ".msg" from ROS called "AdjustXY.msg". I can open this file by right click this from current folder window, and select open as txt. Looks like this
But this is not my final gold. I have to create a excel file. Something that I am able to open with Microsoft Office Excel.
I tried to save this(The "AdjustXY" that I opend as txt) as a ".m" file. I thought this would work. And yes, it does work. But not really. I mean I can open this ".m" file by duble click it. However, I tried to load it. It shows error as"Unknown text on line number 1 of ASCII file AccelCmd.m
"Header"." Then I tried to save it as ".mat". It shows error"Not a binary
MAT-file. Try load -ASCII to read as text."
The reason I want to load the ".m" file because that is the way that I learned. To creat a excel thing. I have to load it first. Then I can set what I want for each row. For example, I want the first line be Adjust. The output in Excel looks like
I also tried with import tools. What I got is like
This is not what I want to see. I mean it is missing "int" befor those "32". Also, I am not sure what should I do to get the output as I expect.
Let me clear this again. The final output should be a Microsoft Office Excel file. And it should be looks like the second figure. I hope this would clear my question. Thank you again for your time. If you still have time, please help me out. Thank you.
Sincerely,
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
Hi Cam
Thank you so much for you time. That does help me a lot. I thought readtable will only work for txt files, therefore I did not tried that. Thank you again.
Hi Cam
Thank you so much for all you did. But just quick question. I am doing this thing. But some of them does not looks like "normal". I mean some of them has long commen word. For that, it is not really I want. If I use the same code to run this, it looks like:
out.PNG
in Excel.
As you can see, it is miss first part. Also, it is really mess. Therefore, if you have time. Will you please take look at it. Also, is there a way that I can code in MATLAB to block those describe word. Only plot what I want?
Sincerely,
Sui
Sui on 29 Jan 2019
Edited: Sui on 29 Jan 2019
Hello Cam
Also I found something that I do not really understand why. For example, I open it by right click select 'open as text' in Matlab. Then I edite like this
q1.PNG
I delecte all commen word by hands. Then I save this file and use the same code to run it. But the output is not really good. I mean it looks like
Q2.PNG
If you do have time, please check out those two common that I just added. Thank you so much.
Sincerely,

Sign in to comment.

Categories

Asked:

Sui
on 17 Jan 2019

Edited:

Sui
on 29 Jan 2019

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!