Robochameleon  v1.0
catstruct.m
Go to the documentation of this file.
1 
2 %
3 % History
4 % Created in 2005
5 % Revisions
6 % 2.0 (sep 2007) removed bug when dealing with fields containing cell
7 % arrays (Thanks to Rene Willemink)
8 % 2.1 (sep 2008) added warning and error identifiers
9 % 2.2 (oct 2008) fixed error when dealing with empty structs (thanks to
10 % Lars Barring)
11 % 3.0 (mar 2013) fixed problem when the inputs were array of structures
12 % (thanks to Tor Inge Birkenes).
13 % Rephrased the help section as well.
14 % 4.0 (dec 2013) fixed problem with unique due to version differences in
15 % ML. Unique(...,'last') is no longer the deafult.
16 % (thanks to Isabel P)
17 function A = catstruct(varargin)
18 
19 
20 error(nargchk(1,Inf,nargin)) ;
21 N = nargin ;
22 
23 if ~isstruct(varargin{end}),
24  if isequal(varargin{end},'sorted'),
25  sorted = 1 ;
26  N = N-1 ;
27  error(nargchk(1,Inf,N)) ;
28  else
29  error('catstruct:InvalidArgument','Last argument should be a structure, or the string "sorted".') ;
30  end
31 else
32  sorted = 0 ;
33 end
34 
35 sz0 = [] ; % used to check that all inputs have the same size
36 
37 % used to check for a few trivial cases
38 NonEmptyInputs = false(N,1) ;
39 NonEmptyInputsN = 0 ;
40 
41 % used to collect the fieldnames and the inputs
42 FN = cell(N,1) ;
43 VAL = cell(N,1) ;
44 
45 % parse the inputs
46 for ii=1:N,
47  X = varargin{ii} ;
48  if ~isstruct(X),
49  error('catstruct:InvalidArgument',['Argument #' num2str(ii) ' is not a structure.']) ;
50  end
51 
52  if ~isempty(X),
53  % empty structs are ignored
54  if ii > 1 && ~isempty(sz0)
55  if ~isequal(size(X), sz0)
56  error('catstruct:UnequalSizes','All structures should have the same size.') ;
57  end
58  else
59  sz0 = size(X) ;
60  end
61  NonEmptyInputsN = NonEmptyInputsN + 1 ;
62  NonEmptyInputs(ii) = true ;
63  FN{ii} = fieldnames(X) ;
64  VAL{ii} = struct2cell(X) ;
65  end
66 end
67 
68 if NonEmptyInputsN == 0
69  % all structures were empty
70  A = struct([]) ;
71 elseif NonEmptyInputsN == 1,
72  % there was only one non-empty structure
73  A = varargin{NonEmptyInputs} ;
74  if sorted,
75  A = orderfields(A) ;
76  end
77 else
78  % there is actually something to concatenate
79  FN = cat(1,FN{:}) ;
80  VAL = cat(1,VAL{:}) ;
81  FN = squeeze(FN) ;
82  VAL = squeeze(VAL) ;
83 
84 
85  [UFN,ind] = unique(FN, 'last') ;
86  % If this line errors, due to your matlab version not having UNIQUE
87  % accept the 'last' input, use the following line instead
88  % [UFN,ind] = unique(FN) ; % earlier ML versions, like 6.5
89 
90  if numel(UFN) ~= numel(FN),
91  warning('catstruct:DuplicatesFound','Fieldnames are not unique between structures.') ;
92  sorted = 1 ;
93  end
94 
95  if sorted,
96  VAL = VAL(ind,:) ;
97  FN = FN(ind,:) ;
98  end
99 
100  A = cell2struct(VAL, FN);
101  A = reshape(A, sz0) ; % reshape into original format
102 end
103 
104 
105 
function catstruct(in varargin)
Concatenate or merge structures with different fieldnames.
function N(in obj)
Retrieve the number of signal components.
Property P
Total signal power (pwr object)
function end(in obj, in k, in n)
Overload of indexing end statement.