Home > matlabmk > comp_struct.m

comp_struct

PURPOSE ^

check two structures for differances - i.e. see if strucutre s1 == structure s2

SYNOPSIS ^

function [fs1, fs2, er] = comp_struct(s1,s2,n1,n2,p,tol)

DESCRIPTION ^

 check two structures for differances - i.e. see if strucutre s1 == structure s2
 function [fs1, fs2, er] = comp_struct(s1,s2,n1,n2,p,tol)

 inputs  6 - 5 optional
 s1      structure one                              class structure
 s2      structure two                              class structure - optional
 n1      first structure name (variable name)       class char - optional
 n2      second structure name (variable name)      class char - optional
 p       pause flag (0 / 1)                         class integer - optional
 tol     tol default tolerance (real numbers)       class integer - optional

 outputs 3 - 3 optional
 fs1     non-matching feilds for structure one      class cell - optional
 fs2     non-matching feilds for structure two      class cell - optional
 er      error flag (value)                         class 

 example:    [r1 r2] = comp_struct(data1,data2,'data1','data2',1)
 michael arant - april 5 2003

 hint:
 passing just one structure causes the program to copy the structure
 and compare the two.  This is an easy way to list the structure

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function [fs1, fs2, er] = comp_struct(s1,s2,n1,n2,p,tol)
0002 % check two structures for differances - i.e. see if strucutre s1 == structure s2
0003 % function [fs1, fs2, er] = comp_struct(s1,s2,n1,n2,p,tol)
0004 %
0005 % inputs  6 - 5 optional
0006 % s1      structure one                              class structure
0007 % s2      structure two                              class structure - optional
0008 % n1      first structure name (variable name)       class char - optional
0009 % n2      second structure name (variable name)      class char - optional
0010 % p       pause flag (0 / 1)                         class integer - optional
0011 % tol     tol default tolerance (real numbers)       class integer - optional
0012 %
0013 % outputs 3 - 3 optional
0014 % fs1     non-matching feilds for structure one      class cell - optional
0015 % fs2     non-matching feilds for structure two      class cell - optional
0016 % er      error flag (value)                         class
0017 %
0018 % example:    [r1 r2] = comp_struct(data1,data2,'data1','data2',1)
0019 % michael arant - april 5 2003
0020 %
0021 % hint:
0022 % passing just one structure causes the program to copy the structure
0023 % and compare the two.  This is an easy way to list the structure
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 % define fs
0033 fs1 = {}; fs2 = {}; er = {};
0034 
0035 % are the variables structures
0036 if isstruct(s1) && isstruct(s2)
0037 %    both structures - get the field names
0038     fn1 = fieldnames(s1);
0039     fn2 = fieldnames(s2);
0040 %    loop through structure 1 and match to structure 2
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 %    get un-matched fields
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 % %    look for non-matched fields fs1 to fs2
0063 %     fs1a = fn1(~pnt1);
0064 %     fs2a = setxor(pnt1,1:length(fn2));
0065 %     if ~isempty(fs2a); fs2a = fs2a(fs2a ~= 0);
0066 %         if ~isempty(fs2a)
0067 %             for kk = 1:length(fs2a)
0068 %                 fs2 = [fs2; {[char(n2) '.' char(fn2(fs2a(kk)))]}];
0069 %                 er = {'Un-matched'}; fs1 = [fs1; {''}];
0070 %             end
0071 %         end
0072 %     end
0073 %     if isempty(fs2); fs2 = cell(0,1); end
0074 %    now evaluate the matching fields  remove "bad" matched
0075 %    loop through structure fields
0076     pnt1i = find(pnt1); pnt2i = find(pnt2);
0077     for ii=1:numel(pnt1i)
0078 %        added loop for indexed structured variables
0079         for jj = 1:size(s1,2)
0080 %            clean display - add index if needed
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 %    one structure, one not
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 %    compare two symbolic expresions
0101 %    direct compare
0102     [ss1 r] = simple(s1); [ss2 r] = simple(s2);
0103     t = isequal(simplify(ss1),simplify(ss2));
0104     if ~t
0105 %        could still be equal, but not able to reduce the symbolic expresions
0106 %        get factors
0107         f1 = findsym(ss1); f2 = findsym(ss2);
0108         w = warning; 
0109         if isequal(f1,f2)
0110 %            same symbolic variables.  same eqn?
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     %            check for match
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 %            now check symbolic equation results
0127             if sum(tres) == length(tres)
0128 %                symbolics assumed to be the same equation
0129                 t = 1;
0130             end
0131         else
0132 %            different symbolic variables
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 %    neither structure - compare
0144     if isequal(s1,s2);
0145         disp(sprintf('%s     %s        match',n1,n2))
0146     else
0147 %        check for "false" failures
0148 %        check structure size
0149         if strcmp(num2str(size(s1)),num2str(size(s2)));
0150 %            structures are same size - check for precision error if numbers
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 %                same size, diferent values or not numbers
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 %                tolerance agreement
0175                 disp(sprintf('%s     %s        tolerance match',n1,n2))
0176             end
0177         else
0178 %            size difference
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 %    fs1 = cell(0,1); fs2 = fs1;
0185 end
0186 
0187 % display non matching fields
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 %                 if isempty(findstr(n1,char(fs1(ii))))
0195 %                     disp(sprintf('First Structure non-matched fields:     %s', ...
0196 %                                     [n1 '.' char(fs1(ii))]))
0197 %                 else
0198 %                     disp(sprintf('First Structure non-matched fields:     %s', ...
0199 %                                     [n1  strrep(char(fs1(ii)),n1,'')]))
0200 %                 end
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 %                 if isempty(findstr(n2,char(fs2(ii))))
0211 %                     disp(sprintf('Second Structure non-matched fields:     %s', ...
0212 %                                     [n2 '.' char(fs2(ii))]))
0213 %                 else
0214 %                     disp(sprintf('Second Structure non-matched fields:     %s', ...
0215 %                                     [n2  strrep(char(fs2(ii)),n2,'')]))
0216 %                 end
0217             end
0218             if p; disp('Paused .....'); pause; end
0219         end
0220     end
0221 end

Generated on Tue 10-May-2016 16:37:45 by m2html © 2005