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
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
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.
0 件のコメント
その他の回答 (1 件)
参考
カテゴリ
Help Center および File Exchange で Structures についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!