-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathcatstruct.m
207 lines (175 loc) · 5.57 KB
/
catstruct.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
function cs = catstruct(varargin)
% catstruct - concatenate structs with any fields
%
% FORMAT: cs = catstruct(s1, s2 [, ... [, fields]])
%
% Input fields:
%
% s1, s2 structs to concatenate
% fields list of fields (in desired order)
%
% Output fields:
%
% cs concatenated struct
%
% Note: if possible catstruct, by default, concatenates along the last
% non-singleton dimension; if that is not possible the structs are
% first squeezed into Sx1 arrays
% Version: v0.9c
% Build: 11111615
% Date: Nov-16 2011, 3:07 PM EST
% Author: Jochen Weber, SCAN Unit, Columbia University, NYC, NY, USA
% URL/Info: http://neuroelf.net/
% Copyright (c) 2011, Jochen Weber
% All rights reserved.
%
% Redistribution and use in source and binary forms, with or without
% modification, are permitted provided that the following conditions are met:
% * Redistributions of source code must retain the above copyright
% notice, this list of conditions and the following disclaimer.
% * Redistributions in binary form must reproduce the above copyright
% notice, this list of conditions and the following disclaimer in the
% documentation and/or other materials provided with the distribution.
% * Neither the name of Columbia University nor the
% names of its contributors may be used to endorse or promote products
% derived from this software without specific prior written permission.
%
% THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
% ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
% WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
% DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
% DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
% (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
% LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
% ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
% (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
% SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
% argument check
if nargin < 2 || ...
~isstruct(varargin{1}) || ...
(~isstruct(varargin{2}) && ...
(nargin > 2 && ...
~iscell(varargin{2})))
error( ...
'neuroelf:BadArgument', ...
'Bad or missing argument.' ...
);
end
narg = nargin;
if iscell(varargin{narg});
fields = varargin{narg};
narg = narg - 1;
else
fields = fieldnames(varargin{1});
end
catd = 0;
stsize = ones(narg, 5);
fm = true;
for ac = 1:narg
if ~isstruct(varargin{ac}) || ...
ndims(varargin{ac}) > 5
error( ...
'neuroelf:BadArgument', ...
'All arguments (but the last) must be of type struct and at most 5-D.' ...
);
end
fn = fieldnames(varargin{ac});
if numel(fn) ~= numel(fields) || ...
~all(strcmp(fn, fields))
fields = uunion(fields, fn);
fm = false;
end
stsize(ac, 1:ndims(varargin{ac})) = size(varargin{ac});
if catd == 0 && ...
max(stsize(ac, :)) > 1 && ...
max(stsize(ac, :)) == prod(stsize(ac, :))
catd = maxpos(stsize(ac, :));
end
end
nf = numel(fields);
if catd == 0
catd = findfirst(any(stsize > 1, 1), -1);
if isempty(catd)
catd = 1;
end
end
if any(any(diff(stsize(:, [1:(catd-1), (catd+1):5])) ~= 0)) || ...
(~fm && ...
all(all(stsize(:, [1:(catd-1), (catd+1):5]) == 1)))
lsq = true;
else
lsq = false;
end
% if all fieldnames match, we can actually use cat!
if fm
% with squeezing
if lsq
% first make a private copy
args = varargin(1:narg);
% squeeze them
for ac = 1:narg
args{ac} = args{ac}(:);
end
% cat
cs = cat(1, args{:});
% reshape?
if catd > 1
cs = reshape(cs, [ones(1, catd - 1), numel(cs)]);
end
% without squeezing
else
% cat in requested dimension
cs = cat(catd, varargin{1:narg});
end
return;
end
% with squeezing
if lsq
% first compute output size
nout = sum(prod(stsize, 2));
% then generate fields containing the data
cs = cell(nf, nout);
% initialize target counter
tc = 1;
% iterate over structs
for ac = 1:narg
% find the field mappings and size of struct
fm = multimatch(fieldnames(varargin{ac}), fields);
ns = numel(varargin{ac});
% and assign in target
cs(fm, tc:(tc+ns-1)) = struct2cell(varargin{ac}(:));
% increase target counter
tc = tc + ns;
end
% finally make a new struct
cs = cell2struct(cs, fields, 1);
% reshape
if catd > 1
cs = reshape(cs, [ones(1, catd - 1), numel(cs)]);
end
% without squeezing
else
% first compute output size
sout = stsize(1, :);
sout(catd) = sum(stsize(:, catd));
% then generate fields containing the data
cs = cell([nf, sout]);
% initialize target counter
tc = 1;
% create assignment flag
af = repmat({':'}, 1, 6);
% iterate over structs
for ac = 1:narg
% find the field mappings and size of struct
fm = multimatch(fieldnames(varargin{ac}), fields);
ns = size(varargin{ac}, catd);
% and assign in target
af{1} = fm(:)';
af{catd+1} = tc:(tc+ns-1);
cs(af{:}) = struct2cell(varargin{ac});
% increase target counter
tc = tc + ns;
end
% finally make a new struct
cs = cell2struct(cs, fields, 1);
end