How to pick the content out of a ROS Point message?
1 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
采纳的回答
Cam Salzberger
2021-8-27
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
0 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Specialized Messages 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!