-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcatstruct.m
97 lines (86 loc) · 2.42 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
function A = catstruct(varargin)
% CATSTRUCT - concatenate structures
%
% X = CATSTRUCT(S1,S2,S3,...) concates the structures S1, S2, ... into one
% structure X.
%
% Example:
% A.name = 'Me' ;
% B.income = 99999 ;
% X = catstruct(A,B)
% % -> X.name = 'Me' ;
% % X.income = 99999 ;
%
% CATSTRUCT(S1,S2,'sorted') will sort the fieldnames alphabetically.
%
% If a fieldname occurs more than once in the argument list, only the last
% occurence is used, and the fields are alphabetically sorted.
%
% To sort the fieldnames of a structure A use:
% A = CATSTRUCT(A,'sorted') ;
%
% To concatenate two similar array of structs use simple concatenation:
% A = dir('*.mat') ; B = dir('*.m') ; C = [A ; B] ;
%
% When there is nothing to concatenate, the result will be an empty
% struct (0x0 struct array with no fields).
%
% See also CAT, STRUCT, FIELDNAMES, STRUCT2CELL
% for Matlab R13 and up
% version 2.2 (oct 2008)
% (c) Jos van der Geest
% email: [email protected]
% History
% Created: 2005
% Revisions
% 2.0 (sep 2007) removed bug when dealing with fields containing cell
% arrays (Thanks to Rene Willemink)
% 2.1 (sep 2008) added warning and error identifiers
% 2.2 (oct 2008) fixed error when dealing with empty structs (Thanks to
% Lars Barring)
N = nargin ;
error(nargchk(1,Inf,N)) ;
if ~isstruct(varargin{end}),
if isequal(varargin{end},'sorted'),
sorted = true;
N = N-1 ;
if N < 1,
A = struct([]) ;
return
end
else
error('catstruct:InvalidArgument','Last argument should be a structure, or the string "sorted".') ;
end
else
sorted = false ;
end
FN = cell(N,1) ;
VAL = cell(N,1) ;
for ii=1:N,
X = varargin{ii} ;
if ~isstruct(X),
error('catstruct:InvalidArgument',['Argument #' num2str(ii) ' is not a structure.']) ;
end
if ~isempty(X),
% empty structs are ignored
FN{ii} = fieldnames(X) ;
VAL{ii} = struct2cell(X) ;
end
end
FN = cat(1,FN{:}) ;
VAL = cat(1,VAL{:}) ;
[UFN,ind] = unique(FN) ;
if numel(UFN) ~= numel(FN),
% warning('catstruct:DuplicatesFound','Duplicate fieldnames found. Last value is used and fields are sorted') ;
sorted = true ;
end
if sorted
VAL = VAL(ind) ;
FN = FN(ind) ;
end
if ~isempty(FN),
% This deals correctly with cell arrays
A = cell2struct(VAL, FN);
else
A = struct([]) ;
end