-
Notifications
You must be signed in to change notification settings - Fork 0
/
flattenStruct.m
49 lines (42 loc) · 1.04 KB
/
flattenStruct.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
function Sf = flattenStruct(S,prefix)
% flattenStruct flatten structs within struct
%
% e.g. S.scalar
% S.struct.scalar
% S.struct.scalar2
%
% becomes
% S.scalar
% S.struct_scalar
% S.struct_scalar2
%
% also handles 1-d struct arrays
if nargin<2,
prefix = '';
end
if ~isempty(prefix),
prefix = [prefix '_'];
end
Sf = struct;
nElem = length(S);
for iE = 1:nElem,
for fn = fieldnames(S)',
if ~isstruct(S(iE).(fn{1})),
Sf(iE).([prefix fn{1}]) = S(iE).(fn{1});
else
tmp = flattenStruct(S(iE).(fn{1}),[prefix fn{1}]);
%uglynes to enable us to cat like this
% https://www.mathworks.com/matlabcentral/newsreader/view_thread/239866
newfields = setdiff(fieldnames(tmp), fieldnames(Sf(iE)));
%reorder according to original order
[~,sidx] = match_str(fieldnames(tmp),newfields);
newfields = newfields(sidx);
%add placeholders
for nfn = newfields(:)',
Sf(iE).(nfn{1}) = [];
end
%finally, copy
Sf(iE) = copyfields(tmp,Sf(iE));
end
end
end