0001 function [fs1, fs2, er] = comp_struct(s1,s2,n1,n2,p,tol)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025 if nargin < 1; help comp_struct; error('I / O error'); end
0026 if nargin < 2; s2 = s1; end
0027 if nargin < 3; n1 = 's1'; end
0028 if nargin < 4; n2 = 's2'; end
0029 if nargin < 5; p = 0; elseif p ~= 1 && p ~= 0; p = 0; end
0030 if nargin < 6; tol = 1e-6; end
0031
0032
0033 fs1 = {}; fs2 = {}; er = {};
0034
0035
0036 if isstruct(s1) && isstruct(s2)
0037
0038 fn1 = fieldnames(s1);
0039 fn2 = fieldnames(s2);
0040
0041 pnt1 = zeros(1,length(fn1));
0042 for ii = 1:length(fn1)
0043 for jj = 1:length(fn2)
0044 if strcmp(fn1(ii),fn2(jj)); pnt1(ii) = jj; end
0045 end
0046 end
0047 pnt2 = zeros(1,length(fn2));
0048 for ii = 1:length(fn2)
0049 for jj = 1:length(fn1)
0050 if strcmp(fn2(ii),fn1(jj)); pnt2(ii) = jj; end
0051 end
0052 end
0053
0054 for ii = find(~pnt1)
0055 fs1 = [fs1; {[char(n1) '.' char(fn1(ii))]}];
0056 fs2 = [fs2; {''}]; er = [er; {'Un-matched'}];
0057 end
0058 for ii = find(~pnt2)
0059 fs2 = [fs2; {[char(n2) '.' char(fn2(ii))]}];
0060 fs1 = [fs1; {''}]; er = [er; {'Un-matched'}];
0061 end
0062
0063
0064
0065
0066
0067
0068
0069
0070
0071
0072
0073
0074
0075
0076 pnt1i = find(pnt1); pnt2i = find(pnt2);
0077 for ii=1:numel(pnt1i)
0078
0079 for jj = 1:size(s1,2)
0080
0081 if size(s1,2) == 1
0082 n1p = [n1 '.' char(fn1(pnt1i(ii)))];
0083 n2p = [n2 '.' char(fn2(pnt2i(ii)))];
0084 else
0085 n1p = [n1 '(' num2str(jj) ').' char(fn1(ii))]; ...
0086 n2p = [n2 '(' num2str(jj) ').' char(fn2(pnt2(ii)))];
0087 end
0088 [fss1 fss2 err] = comp_struct(getfield(s1(jj),char(fn1(pnt1i(ii)))), ...
0089 getfield(s2(jj),char(fn2(pnt2i(ii)))),n1p,n2p,p);
0090 if ~iscell(err); err = cellstr(err); end
0091 fs1 = [fs1; fss1]; fs2 = [fs2; fss2]; er = [er; err];
0092 end
0093 end
0094 elseif isstruct(s1) ~= isstruct(s2)
0095
0096 disp(sprintf('%s %s Type mismatch - NOT equal',n1,n2));
0097 fs1 = cell(0,1); fs2 = fs1;
0098 if p; disp('Paused .....'); pause; end
0099 elseif isa(s1,'sym') && isa(s2,'sym')
0100
0101
0102 [ss1 r] = simple(s1); [ss2 r] = simple(s2);
0103 t = isequal(simplify(ss1),simplify(ss2));
0104 if ~t
0105
0106
0107 f1 = findsym(ss1); f2 = findsym(ss2);
0108 w = warning;
0109 if isequal(f1,f2)
0110
0111 temp = [1 findstr(f1,' ') + 1]; tres = NaN * zeros(1,30);
0112 for jj = 1:1e3
0113 ss1e = ss1; ss2e = ss2;
0114 for ii = 1:length(temp);
0115 tv = (real(rand^rand)) / (real(rand^rand));
0116 ss1e = subs(ss1e,f1(temp(ii)),tv);
0117 ss2e = subs(ss2e,f2(temp(ii)),tv);
0118 end
0119
0120 if isnan(ss1e) || isnan(ss2e)
0121 tres(jj) = 1;
0122 elseif (double(ss1e) - double(ss2e)) < tol
0123 tres(jj) = 1;
0124 end
0125 end
0126
0127 if sum(tres) == length(tres)
0128
0129 t = 1;
0130 end
0131 else
0132
0133 end
0134 warning(w)
0135 end
0136 if t
0137 disp(sprintf('%s %s match',n1,n2))
0138 else
0139 disp(sprintf('%s %s do NOT match',n1,n2))
0140 fs1 = n1; fs2 = n2; er = 'Symbolic disagreement';
0141 end
0142 else
0143
0144 if isequal(s1,s2);
0145 disp(sprintf('%s %s match',n1,n2))
0146 else
0147
0148
0149 if strcmp(num2str(size(s1)),num2str(size(s2)));
0150
0151 if ischar(s1) || iscell(s1) || ...
0152 (max(max(max(abs(s1 - s2)))) > tol * (max(max(max([s1 s2]))) ...
0153 - min(min(min([s1 s2])))))
0154
0155 disp(sprintf('%s %s do NOT match',n1,n2))
0156 fs1 = [fs1; {n1}]; fs2 = [fs2; {n2}]; er = [er; {'?'}];
0157 if ischar(s1)
0158 er1 = sprintf('%s is char - %s',n1,char(s1)); disp(er1);
0159 end
0160 if ischar(s2)
0161 er2 = sprintf('%s is char - %s',n2,char(s2)); disp(er2);
0162 end
0163 if exist('er1','var') && exist('er2','var')
0164 er = [er; {[er1 ' ---> ' er2]}];
0165 end
0166 if ~ischar(s1) && ~iscell(s1);
0167 er = sprintf('Max error (%%) = %g%%', ...
0168 max(max(max(abs(s1 - s2)))) / ...
0169 (max(max(max([s1 s2]))) - min(min(min([s1 s2])))));
0170 disp(er);
0171 end
0172 if p; disp('Paused .....'); pause; end
0173 else
0174
0175 disp(sprintf('%s %s tolerance match',n1,n2))
0176 end
0177 else
0178
0179 disp(sprintf('%s %s do NOT match - DIFFERENT SIZES',n1,n2))
0180 fs1 = [fs1; {n1}]; fs2 = [fs2; {n2}]; er = [er; {'String size error'}];
0181 if p; disp('Paused .....'); pause; end
0182 end
0183 end
0184
0185 end
0186
0187
0188 if 1
0189 if ~isempty(fs1)
0190 for ii = 1:length(fs1)
0191 if strcmp(n1,fs1(ii))
0192 disp(sprintf('First Structure non-matched fields: %s',[n1]))
0193 else
0194
0195
0196
0197
0198
0199
0200
0201 end
0202 if p; disp('Paused .....'); pause; end
0203 end
0204 end
0205 if ~isempty(fs2)
0206 for ii = 1:length(fs2)
0207 if strcmp(n2,fs2(ii))
0208 disp(sprintf('Second Structure non-matched fields: %s',[n2]))
0209 else
0210
0211
0212
0213
0214
0215
0216
0217 end
0218 if p; disp('Paused .....'); pause; end
0219 end
0220 end
0221 end