function disp_diff(A,B,s1,s2) % disp_diff(A,B) % disp_diff(A,B,s1,s2) % % Display recursively all differences between matrices A and B. % A and B may be nested structures and cell arrays. % s1 and s2 are optional names of the arguments. % Only numeric, string, structures, and cell types supported. % Jan Mandel, 2001 last updated Jan 16 2002 % % This software is available under GNU General Public License. % It may be distributed free of charge only, with the source form included, % the original source acknowledged, and this license notice included. % See http://www.gnu.org/copyleft/gpl.html for details. % There is no warranty of any kind. if ~exist('s1','var'), s1=inputname(1); end if isempty(s1), s1='arg1'; end if ~exist('s2','var'), s2=inputname(2); end if isempty(s2), s2='arg2'; end if isstruct(A), if ~isstruct(B), fprintf('%s is not a structure\n',s2) else f=fieldnames(A); for i=1:length(f), if isfield(B,f{i}), disp_diff(getfield(A,f{i}),getfield(B,f{i}),[s1,'.',f{i}],[s2,'.',f{i}]) else fprintf('%s.%s does not exist\n',s2,f{i}) end end f=fieldnames(B); for i=1:length(f), if ~isfield(A,f{i}), fprintf('%s.%s does not exist\n',s1,f{i}) end end end elseif iscell(A), if iscell(B), % fprintf('comparing cell %s\n',s) ma=size(A); mb=size(B); if all(ma == mb), for i=1:ma(1), for j=1:ma(2), disp_diff(A{i,j},B{i,j},sprintf('%s{%i,%i}',s1,i,j),sprintf('%s{%i,%i}',s2,i,j)) end end else fprintf('size differs: %s [%i %i] %s [%i %i]\n',s1,ma,s2,mb) end else fprintf('%s is not a cell') end elseif isnumeric(A), if isnumeric(B), % fprintf('comparing numeric %s\n',s) ma=size(A); mb=size(B); difdim=1; if all(size(ma) == size(mb)), if all(ma == mb) difdim=0; if all(ma > 0), d=big(A-B); nd=full(sum(sum(A~=B))); nz=max(nnz(A),nnz(B)); if d, fprintf(['%s %s size %i %i: difference %g relative %g\n',... ' matrices differ in %g place(s) from %g nonzeros\n'],... s1,s2,ma,d,d/max(big(A),big(B)),nd,nz) end end end end if difdim, fprintf('size differs: %s [%s] %s [%s]\n',s1,num2str(ma),s2,num2str(mb)) end else fprintf('%s is not numeric',s2) end elseif ischar(A), if ischar(B), if ~strcmp(A,B), fprintf('strings differ: %s=%s %s=%s\n',s1,A,s2,B) end end else fprintf('%s: unsupported matrix type\n',s1) end return function m=big(a) m=full(max(abs(a(:)))); return