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:
----------->Velocity , Position, Angles
----------->Velocity , Position, Angles
. X2000
----------->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
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 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 = {...
DataRobot = struct();
for k = 1:numel(C)
tmp = struct('Velocity',sqrt(k),'Position',k,'Angles',k^2);
DataRobot.(C{k}) = tmp;
Method One: structfun
>> structfun(@(s)s.Velocity,DataRobot)
ans =
or if the fields contain non-scalar arrays:
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 =
>> {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 2016년 7월 20일
Perfect it works well, Thank you very much
  댓글 수: 1
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