Collect data in structure which contain structure with timestamps

조회 수: 8(최근 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 :)

댓글을 달려면 로그인하십시오.

범주

Find more on Variables in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by