How do I extract values of fields in a multi-level structure in MATLAB 8.1(R2013a)?
2 views (last 30 days)
Show older comments
MathWorks Support Team
on 25 Oct 2013
Edited: MathWorks Support Team
on 21 Apr 2014
I have a structure converted from xml file. Now I want to extract data from the structure and store them in an array. Now the problem is taht the structure is quite big and contains many substructures underneath like the following. I wonder how to extract all values from it? Does it need a loop?
s.a=6;
s.b=rand(5);
s.s1.c='hello';
bigs=s;
bigs.xy=[0.1 0.5];
Accepted Answer
MathWorks Support Team
on 24 Oct 2013
If you know the whole construction of the structure, please refer to the related solution listed below.
But if you don't know how many levels included in the structure, a WHILE loop would be suitable for the job. Additionally, the data types of different fields usually are not all the same, so store the values in cells first could be safer for postprocessing the data. The attached example expand the structure regardless the field names and save all values in cells. Please modify the code accordingly to meet your need.
1 Comment
Walter Roberson
on 28 Nov 2013
That code is broken. When you do the s(i)=[]; then you must not increment "i" or else you skip the data that "fell down into" s(i)
More Answers (0)
See Also
See Also
Categories
Find more on Simple Batch Processing 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!