rbmatlab  1.16.09
 All Classes Namespaces Files Functions Variables Modules Pages
structcmp.m
1 function [iseq, diffs, add1, add2] = structcmp(s1,s2,ignorelist)
2 %function [iseq, diffs, add1, add2] = structcmp(s1,s2,ignorelist);
3 %
4 % compare two structures s1 and s2 and ignoring fields given in the
5 % cell-array of fielnames ignorelist.
6 % iseq = 1 if the structures are identical except ignorelist
7 % diffs is a cell array of the fieldnames persistent in both
8 % structs but differing, add1 are the fields existing in s1 but
9 % not in s2, add2 is a cell array of fieldnames existing in
10 % s2 but not in s1
11 % function handles are ignored automatically, as they cannot be compared
12 
13 % Bernard Haasdonk 23.5.2007
14 
15 diffs = {};
16 
17 if (nargin<3) || isempty(ignorelist)
18  ignorelist = {};
19 end;
20 
21 f1 = fieldnames(s1);
22 f1 = setdiff(f1, ignorelist);
23 f2 = fieldnames(s2);
24 f2 = setdiff(f2, ignorelist);
25 add1 = setdiff(f1,f2);
26 add2 = setdiff(f2,f1);
27 fi = intersect(f1,f2);
28 
29 for i = 1:length(fi)
30  if isa(s1.(fi{i}), 'function_handle')
31  if ~isequal(func2str(s1.(fi{i})), func2str(s2.(fi{i})))
32  diffs = [ diffs; {fi(i)}];
33  end
34  elseif iscell(s1.(fi{i})) ...
35  && all(cellfun(@(x) isa(x, 'function_handle'), s1.(fi{i})))
36  strings1 = cellfun(@func2str, s1.(f1{i}), 'UniformOutput', false);
37  strings2 = cellfun(@func2str, s1.(f1{i}), 'UniformOutput', false);
38  if ~isequal(strings1, strings2)
39  diffs = [ diffs; {fi(i)} ];
40  end
41  elseif ~isequal(s1.(fi{i}),s2.(fi{i}))
42  diffs = [diffs; {fi(i)}];
43  end
44 end;
45 
46 iseq = 1;
47 if ~isempty(diffs) || ...
48  ~isempty(add1) || ...
49  ~isempty(add2)
50  iseq = 0;
51 end;
52 %| \docupdate