Collect data in structure which contain structure with timestamps

2 次查看(过去 30 天)
I'm faced to a problem on Matlab, I have a structure DataRobot which contain around 2000 structures with in it 3 datas like this:
DataRobot
------>x0x32_016_0x2D_Jul_0x2D_13_0x20_17_0x3A_30_0x3A_18_0x2E_840671
----------->Velocity , Position, Angles
------>x0x32_016_0x2D_Jul_0x2D_13_0x20_17_0x3A_30_0x3A_19_0x2E_015942
----------->Velocity , Position, Angles
...
..
. X2000
..
...
------>x0x32_016_0x2D_Jul_0x2D_13_0x20_17_0x3A_30_0x3A_19_0x2E_176554
----------->Velocity , Position, Angles
I would like to collect all the velocity in a simple array but my problem is that the name of the structure
:x0x32_016_0x2D_Jul_0x2D_13_0x20_17_0x3A_30_0x3A_19_0x2E_176554
correspond to a timestamps that I don't really care but I dont wanna write by hand the name of the 2000 timestamps. Is any simple way to collect all my velocity datas?
Thank you very much
  1 个评论
Azzi Abdelmalek
Azzi Abdelmalek 2016-7-20
Make your question clear, how your data are stored? in a text file? post a sample of your data then explain what is your problem

请先登录,再进行评论。

采纳的回答

Stephen23
Stephen23 2016-7-20
编辑:Stephen23 2016-7-20
Although it is not completely clear, you seem to be describing a scalar structure with multiple fields, each field contains one nested scalar structure with three fields.
First we replicate the nested structures for demonstrating the methods below:
C = {...
'x0x32_016_0x2D_Jul_0x2D_13_0x20_17_0x3A_30_0x3A_18_0x2E_840671';...
'x0x32_016_0x2D_Jul_0x2D_13_0x20_17_0x3A_30_0x3A_19_0x2E_015942';...
'x0x32_016_0x2D_Jul_0x2D_13_0x20_17_0x3A_30_0x3A_19_0x2E_176554';...
};
DataRobot = struct();
for k = 1:numel(C)
tmp = struct('Velocity',sqrt(k),'Position',k,'Angles',k^2);
DataRobot.(C{k}) = tmp;
end
Method One: structfun
>> structfun(@(s)s.Velocity,DataRobot)
ans =
1.0000
1.4142
1.7321
or if the fields contain non-scalar arrays:
structfun(@(s){s.Velocity},DataRobot)
Method Two: non-scalar structure
Accessing data is trivial when the data has been designed well. In this case there is no real purpose in nesting the structures, and a non-scalar structure would make accessing the data much simpler. So when the structure is flattened into a simple non-scalar structure then your question can be resolved very very simply!
We de-nest these nested structures into a non-scalar array (much easier to work with), and then we can easily extract whatever data you need:
tmp = fieldnames(DataRobot);
S = cell2mat(struct2cell(DataRobot));
[S.Timestamp] = tmp{:};
Now it is easy to access any data:
>> [S.Velocity]
ans =
1.0000 1.4142 1.7321
You can also access the timetamps in the non-scalar structure:
>> S(2).Timestamp
ans =
x0x32_016_0x2D_Jul_0x2D_13_0x20_17_0x3A_30_0x3A_19_0x2E_015942
>> {S.Timestamp}
ans =
[1x62 char] [1x62 char] [1x62 char]
Note that accessing the data in a non-scalar structure is significantly faster and simpler than with the nested structures. See the link I gave for more information on non-scalar structures.

更多回答(1 个)

Alex
Alex 2016-7-20
Perfect it works well, Thank you very much
  1 个评论
Stephen23
Stephen23 2016-7-20
编辑:Stephen23 2016-7-20
@Alex: you can also accept my answer, if it solves your question. Accepting answers keeps the volunteers happy :)

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Structures 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by