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

2 ビュー (過去 30 日間)
Andreas Schwager
Andreas Schwager 2021 年 8 月 27 日
回答済み: Cam Salzberger 2021 年 8 月 27 日
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

採用された回答

Cam Salzberger
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 件)

カテゴリ

Help Center および File ExchangeSpecialized Messages についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by