rbmatlab 0.10.01
general/basic/structcmp.m
00001 function [iseq, diffs, add1, add2] = structcmp(s1,s2,ignorelist)
00002 %function [iseq, diffs, add1, add2] = structcmp(s1,s2,ignorelist);
00003 %
00004 % compare two structures s1 and s2 and ignoring fields given in the
00005 % cell-array of fielnames ignorelist.
00006 % iseq = 1 if the structures are identical except ignorelist
00007 % diffs is a cell array of the fieldnames persistent in both
00008 % structs but differing, add1 are the fields existing in s1 but
00009 % not in s2, add2 is a cell array of fieldnames existing in
00010 % s2 but not in s1
00011 % function handles are ignored automatically, as they cannot be compared
00012 
00013 % Bernard Haasdonk 23.5.2007
00014 
00015 diffs = {};
00016 
00017 if (nargin<3) || isempty(ignorelist)
00018   ignorelist = {};
00019 end;
00020 
00021 f1 = fieldnames(s1);
00022 f1 = setdiff(f1, ignorelist);
00023 f2 = fieldnames(s2);
00024 f2 = setdiff(f2, ignorelist);
00025 add1 = setdiff(f1,f2);
00026 add2 = setdiff(f2,f1);
00027 fi = intersect(f1,f2);
00028 
00029 for i = 1:length(fi)
00030   if isa(s1.(fi{i}), 'function_handle')
00031     if ~isequal(func2str(s1.(fi{i})), func2str(s2.(fi{i})))
00032       diffs = [ diffs; {fi(i)}];
00033     end
00034   elseif iscell(s1.(fi{i})) ...
00035            && all(cellfun(@(x) isa(x, 'function_handle'), s1.(fi{i})))
00036     strings1 = cellfun(@func2str, s1.(f1{i}), 'UniformOutput', false);
00037     strings2 = cellfun(@func2str, s1.(f1{i}), 'UniformOutput', false);
00038     if ~isequal(strings1, strings2)
00039       diffs = [ diffs; {fi(i)} ];   
00040     end
00041   elseif ~isequal(s1.(fi{i}),s2.(fi{i}))
00042     diffs = [diffs; {fi(i)}];
00043   end
00044 end;
00045 
00046 iseq = 1;
00047 if ~isempty(diffs) ||  ...
00048       ~isempty(add1) || ...
00049       ~isempty(add2)  
00050   iseq = 0;
00051 end;
00052 %| \docupdate 
 All Classes Namespaces Files Functions Variables