Home > matlabmk > comp_struct_quiet.m

comp_struct_quiet

PURPOSE ^

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

SYNOPSIS ^

function [fs1, fs2, er] = comp_struct_quiet(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_quiet(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_quiet(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

 Note, this code, downloaded from the Mathworks filesharing website, has
 been slightly modified by DG to suppress all command line reports.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function [fs1, fs2, er] = comp_struct_quiet(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_quiet(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_quiet(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 % Note, this code, downloaded from the Mathworks filesharing website, has
0026 % been slightly modified by DG to suppress all command line reports.
0027 
0028 if nargin < 1; help comp_struct_quiet; error('I / O error'); end
0029 if nargin < 2; s2 = s1; end
0030 if nargin < 3; n1 = 's1'; end
0031 if nargin < 4; n2 = 's2'; end
0032 if nargin < 5; p = 0; elseif p ~= 1 && p ~= 0; p = 0; end
0033 if nargin < 6; tol = 1e-6; end
0034 
0035 % define fs
0036 fs1 = {}; fs2 = {}; er = {};
0037 
0038 % are the variables structures
0039 if isstruct(s1) && isstruct(s2)
0040 %    both structures - get the field names
0041     fn1 = fieldnames(s1);
0042     fn2 = fieldnames(s2);
0043 %    loop through structure 1 and match to structure 2
0044     pnt1 = zeros(1,length(fn1));
0045     for ii = 1:length(fn1)
0046         for jj = 1:length(fn2)
0047             if strcmp(fn1(ii),fn2(jj)); pnt1(ii) = jj; end
0048         end
0049     end
0050     pnt2 = zeros(1,length(fn2));
0051     for ii = 1:length(fn2)
0052         for jj = 1:length(fn1)
0053             if strcmp(fn2(ii),fn1(jj)); pnt2(ii) = jj; end
0054         end
0055     end
0056 %    get un-matched fields
0057     for ii = find(~pnt1)
0058         fs1 = [fs1; {[char(n1) '.' char(fn1(ii))]}];
0059         fs2 = [fs2; {''}]; er = [er; {'Un-matched'}];
0060     end
0061     for ii = find(~pnt2)
0062         fs2 = [fs2; {[char(n2) '.' char(fn2(ii))]}];
0063         fs1 = [fs1; {''}]; er = [er; {'Un-matched'}];
0064     end
0065 % %    look for non-matched fields fs1 to fs2
0066 %     fs1a = fn1(~pnt1);
0067 %     fs2a = setxor(pnt1,1:length(fn2));
0068 %     if ~isempty(fs2a); fs2a = fs2a(fs2a ~= 0);
0069 %         if ~isempty(fs2a)
0070 %             for kk = 1:length(fs2a)
0071 %                 fs2 = [fs2; {[char(n2) '.' char(fn2(fs2a(kk)))]}];
0072 %                 er = {'Un-matched'}; fs1 = [fs1; {''}];
0073 %             end
0074 %         end
0075 %     end
0076 %     if isempty(fs2); fs2 = cell(0,1); end
0077 %    now evaluate the matching fields  remove "bad" matched
0078 %    loop through structure fields
0079     pnt1i = find(pnt1); pnt2i = find(pnt2);
0080     for ii=1:numel(pnt1i)
0081 %        added loop for indexed structured variables
0082         for jj = 1:size(s1,2)
0083 %            clean display - add index if needed
0084             if size(s1,2) == 1
0085                 n1p = [n1 '.' char(fn1(pnt1i(ii)))];
0086                 n2p = [n2 '.' char(fn2(pnt2i(ii)))];
0087             else
0088                 n1p = [n1 '(' num2str(jj) ').' char(fn1(ii))]; ...
0089                             n2p = [n2 '(' num2str(jj) ').' char(fn2(pnt2(ii)))];
0090             end
0091             [fss1 fss2 err] = comp_struct_quiet(getfield(s1(jj),char(fn1(pnt1i(ii)))), ...
0092                                 getfield(s2(jj),char(fn2(pnt2i(ii)))),n1p,n2p,p);
0093             if ~iscell(err); err = cellstr(err); end
0094             fs1 = [fs1; fss1]; fs2 = [fs2; fss2]; er = [er; err];
0095         end
0096     end
0097 elseif isstruct(s1) ~= isstruct(s2)
0098 %    one structure, one not
0099     disp(sprintf('%s    %s        Type mismatch - NOT equal',n1,n2));
0100     fs1 = cell(0,1); fs2 = fs1;
0101     if p; disp('Paused .....'); pause; end
0102 elseif isa(s1,'sym') && isa(s2,'sym')
0103 %    compare two symbolic expresions
0104 %    direct compare
0105     [ss1 r] = simple(s1); [ss2 r] = simple(s2);
0106     t = isequal(simplify(ss1),simplify(ss2));
0107     if ~t
0108 %        could still be equal, but not able to reduce the symbolic expresions
0109 %        get factors
0110         f1 = findsym(ss1); f2 = findsym(ss2);
0111         w = warning; 
0112         if isequal(f1,f2)
0113 %            same symbolic variables.  same eqn?
0114             temp = [1 findstr(f1,' ') + 1]; tres = NaN * zeros(1,30);
0115             for jj = 1:1e3
0116                 ss1e = ss1; ss2e = ss2;
0117                 for ii = 1:length(temp);
0118                     tv = (real(rand^rand)) / (real(rand^rand));
0119                     ss1e = subs(ss1e,f1(temp(ii)),tv);
0120                     ss2e = subs(ss2e,f2(temp(ii)),tv);
0121                 end
0122     %            check for match
0123                 if isnan(ss1e) || isnan(ss2e)
0124                     tres(jj) = 1;
0125                 elseif (double(ss1e) - double(ss2e)) < tol
0126                         tres(jj) = 1;
0127                 end
0128             end
0129 %            now check symbolic equation results
0130             if sum(tres) == length(tres)
0131 %                symbolics assumed to be the same equation
0132                 t = 1;
0133             end
0134         else
0135 %            different symbolic variables
0136         end
0137         warning(w)
0138     end
0139     if t
0140         %disp(sprintf('%s    %s        match',n1,n2)) DG Change ??
0141     else
0142         disp(sprintf('%s    %s        do NOT match',n1,n2))
0143         fs1 = n1; fs2 = n2; er = 'Symbolic disagreement';
0144     end
0145 else
0146 %    neither structure - compare
0147     if isequal(s1,s2);
0148         %disp(sprintf('%s     %s        match',n1,n2)) DG Change ??
0149     else
0150 %        check for "false" failures
0151 %        check structure size
0152         if strcmp(num2str(size(s1)),num2str(size(s2)));
0153 %            structures are same size - check for precision error if numbers
0154             if ischar(s1) || iscell(s1) || ...
0155                 (max(max(max(abs(s1 - s2)))) > tol * (max(max(max([s1 s2]))) ...
0156                 - min(min(min([s1 s2])))))
0157 %                same size, diferent values or not numbers
0158                 disp(sprintf('%s    %s        do NOT match',n1,n2))
0159                 fs1 = [fs1; {n1}]; fs2 = [fs2; {n2}]; er = [er; {'?'}];
0160                 if ischar(s1)
0161                     er1 = sprintf('%s is char - %s',n1,char(s1)); disp(er1);
0162                 end
0163                 if ischar(s2)
0164                     er2 = sprintf('%s is char - %s',n2,char(s2)); disp(er2);
0165                 end
0166                 if exist('er1','var') && exist('er2','var')
0167                     er = [er; {[er1 ' ---> ' er2]}];
0168                 end
0169                 if ~ischar(s1) && ~iscell(s1);
0170                     er = sprintf('Max error (%%) = %g%%', ...
0171                             max(max(max(abs(s1 - s2)))) / ...
0172                             (max(max(max([s1 s2]))) - min(min(min([s1 s2])))));
0173                     disp(er);
0174                 end
0175                 if p; disp('Paused .....'); pause; end
0176             else
0177 %                tolerance agreement
0178                 %disp(sprintf('%s     %s        tolerance match',n1,n2)) DG Change ??
0179             end
0180         else
0181 %            size difference
0182             disp(sprintf('%s    %s        do NOT match - DIFFERENT SIZES',n1,n2))
0183             fs1 = [fs1; {n1}]; fs2 = [fs2; {n2}]; er = [er; {'String size error'}];
0184             if p; disp('Paused .....'); pause; end
0185         end
0186     end
0187 %    fs1 = cell(0,1); fs2 = fs1;
0188 end
0189 
0190 % display non matching fields
0191 if 1
0192     if ~isempty(fs1)
0193         for ii = 1:length(fs1)
0194             if strcmp(n1,fs1(ii))
0195                 disp(sprintf('First Structure non-matched fields:     %s',[n1]))
0196             end
0197             if p; disp('Paused .....'); pause; end
0198         end
0199     end
0200     if ~isempty(fs2)
0201         for ii = 1:length(fs2)
0202             if strcmp(n2,fs2(ii))
0203                 disp(sprintf('Second Structure non-matched fields:     %s',[n2]))
0204             end
0205             if p; disp('Paused .....'); pause; end
0206         end
0207     end
0208 end

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