How to extract data from a multi layered structure
Show older comments
Hi,
I have a multi-layered structure A that include 3 structures (a, b and c) and and all of them contain 10 fieldnames with 10 rows and every cell further contains 10 double numbers mixed with nans.
Doing
A.b.fieldname1
yields the content of the first cell (10 double), but I cant seem to to be able to extract the entire fielname. Doing
c = cell2mat(struct2cell(A.b.fieldname1(1,1)))
yields the following error: Expected one output from a curly brace or dot indexing expression, but there were 10 results.
So how can I extract the data from this multi-layered structure ?
Thank you,
6 Comments
Stephen23
on 22 Aug 2019
"So how can I extract the data from this multi-layered structure ?"
Using loops.
You forgot to tell us some very important information: what sizes are A, a, b, and c ?
012786534
on 22 Aug 2019
This is a bit confusing. My reading of your question is that you have a scalar structure A with 3 fields, themselves scalar structures, each with 10 fields. After that it's not clear whether these fields contain matrices or cell arrays. An example of such a structure may be:
%using only two subfields, and two sub-subfields
%with matrices in the fields
A.a.f1 = rand(10, 10);
A.a.f2 = magic(10);
a.b.f1 = ones(10);
a.b.f2 = eyes(10);
%with cell arrays of vectors in the fields
B.a.f1 = num2cell(rand(10), 2);
B.a.f2 = num2cell(magic(10), 2);
B.b.f1 = num2cell(ones(10), 2);
B.b.f2 = num2cell(eyes(10), 2);
In this A (or B) is scalar (size 1x1)
However, your example shows a structure array (size 1x10), which is not the same thing at all.
So what do you have? You can always attach a mat file with the structure so we know for sure.
"My reading of your question is that you have a scalar structure A with 3 fields, themselves scalar structures, each with 10 fields"
If that were the case, then the error message "Expected one output from a curly brace or dot indexing expression..." would not occur. One or more of the structures is non-scalar. As far as I can tell, the original explanation simply omits this important information.
"Lets say I had a structure like this..:"
But in your question you clearly describe nested structures.
With nested structures, you will have to use loops (implicitly or explicitly).
If you really had a structure "like this" (without nested structures), then you could simply use arrayfun or comma-separated list syntax to access your data, no loops required:
Without answering our questions or providing sample data, we have to rely on guessing. Guessing is not a reliable way for you to get help.
012786534
on 22 Aug 2019
Your structure is non-scalar:
>> load('PMAN_climatology.mat')
>> size(pman_climatology)
ans =
1 14
That explains the error message you give in your question.
Not only that, but none of its fields contain nested structures:
>> F = @(s)any(structfun(@isstruct,s));
>> arrayfun(F,pman_climatology)
ans =
0 0 0 0 0 0 0 0 0 0 0 0 0 0
"How can i calculate the minimum of a given field (min_temp for example)?"
You don't define "the minimum" for mutliple vectors: do you want the global minimum, or the minimum of each vector, or the minimum of each column when the vectors are concatenated into one matrix, or some other minimum?
Answers (1)
"How can i calculate the minimum of a given field (min_temp for example) ?"
You do not define what "the minimum" means for multiple vectors, which makes it hard to guesss what output you expect...
Method one: comma-separated list and cellfun:
For example:
>> vec = cellfun(@min,{pman_climatology.min_temp})
vec =
1.0000 4.6000 3.0000 3.0000 2.1000 2.5000 2.0000 2.0000 6.5000 1.5000 1.0000 1.5000 4.1000 1.0000
That gives the minimum value of the field min_temp for each element of the structure pman_climatology. Compare with the first few elements of pman_climatology:
>> min(pman_climatology(1).min_temp)
ans =
1
>> min(pman_climatology(2).min_temp)
ans =
4.6000
>> min(pman_climatology(3).min_temp)
ans =
3
Method two: anonymous function and arrayfun:
>> F = @(s)min(s.min_temp);
>> arrayfun(F,pman_climatology)
Categories
Find more on Structures 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!