MATLAB Answers

How to pick the content out of a ROS Point message?

1 view (last 30 days)
Hello,
How to pick the content out of a ROS Point message?
Or how to get members out of a cell arry into a simple array?
The following code runs, but is there any way avoiding the for-loop and cpoying the data direct from cell array member to an array?
Euler = readMessages(BagFile,startIdx:endIdx);
for ii = 1:size(Euler,1)
yaw(ii) = Euler{ii}.Point.X;
pitch(ii) = Euler{ii}.Point.Y;
roll(ii) = Euler{ii}.Point.Z;
end

Accepted Answer

Cam Salzberger
Cam Salzberger on 27 Aug 2021
Hello Andreas,
cellfun is useful for operations like this, where you want to do the same thing on each cell. Probably the best and most efficient code would look something like this:
% Use struct message format for faster read time
Euler = readMessages(BagFile,startIdx:endIdx,"DataFormat","struct");
[yaw, pitch, roll] = cellfun(@getXYZFromPoint, Euler);
function [x, y, z] = getXYZFromPoint(msg)
% Extract internal message first for efficiency
% (matters more for more nested messages)
vec = msg.Point;
x = vec.X;
y = vec.Y;
z = vec.Z;
end
If on the other hand you wanted to turn the cell array of messages (or message structs) into a message object or struct array, you could just do this:
EulerArr = [Euler{:}];
Note that all messages have to have the same structure for that to work.
-Cam

More Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by