Diff a struct

  • Follow


Is there a way (command/s) to diff a structure similar to diff'ing files on a unix/linux system?  I have a several structures with dozens of fields, both strings & numbers, and I'd like a quick and simple way of findings which fields are different and what those differences are.  Any suggestions?

Thanks,
dlh
0
Reply DLH 8/5/2010 2:00:05 PM

DLH wrote:
> Is there a way (command/s) to diff a structure similar to diff'ing files 
> on a unix/linux system?  I have a several structures with dozens of 
> fields, both strings & numbers, and I'd like a quick and simple way of 
> findings which fields are different and what those differences are.  Any 
> suggestions?

No such routine is provided with Matlab (or at least I've never seen one.)

A routine like this would not be all that difficult to write, but you 
would have to decide on a couple of things:

- breadth first search, or depth first search?

- if the field order is different but the same fields are there, then is 
this a "difference" that should be noted?

- do you want something special to happen for class variables, since 
reading from such a variable can theoretically provoke an action ?

- how do you want to handle class variables that have the same 
properties but have unique instance numbers? For example,

A = handle(line(0,0));
B = handle(line(0,0));

Should "A" and "B" compare the same or different?

- Case-sensitive string comparisons? Should it depend upon the variable?

- is blank padding significant for strings? Leading or trailing? Or 
should trailing blanks not be significant for character arrays (since 
all the rows must be the same length and a difference in some other row 
might be forcing this row to be longer), or should trailing blanks be 
significant for character arrays if blanks are detected at the end of 
every row (which might imply deliberate allocation as a longer array)

- should unicode 8764 ("mathematical tilde operator") compare the same 
as '~', should the unicode ellipsis character compare the same as '...', 
should unicode m-dash and n-dash compare the same; and so on for other 
strong glyph similarities?

- has Walter confused you enough yet?

-
0
Reply Walter 8/5/2010 2:50:31 PM


Walter,

Thanks for your response, and no you have not confused me yet.

The points you raised are valid, and should be handled appropriately for a general algorithm.  For my immediate purposes I merely want to perform a case-insensitive search, listing the differences in the fields common to both (or more) structures while ignoring whitespace.  Field order is irrelevant, and although I expect all my structures to have the same fields the algorithm should detect and report any uncommon (extra or missing) fields.

Thanks.
__
dlh

Walter Roberson <roberson@hushmail.com> wrote in message <YcA6o.1205$1v3.582@newsfe20.iad>...
> DLH wrote:
> > Is there a way (command/s) to diff a structure similar to diff'ing files 
> > on a unix/linux system?  I have a several structures with dozens of 
> > fields, both strings & numbers, and I'd like a quick and simple way of 
> > findings which fields are different and what those differences are.  Any 
> > suggestions?
> 
> No such routine is provided with Matlab (or at least I've never seen one.)
> 
> A routine like this would not be all that difficult to write, but you 
> would have to decide on a couple of things:
> 
> - breadth first search, or depth first search?
> 
> - if the field order is different but the same fields are there, then is 
> this a "difference" that should be noted?
> 
> - do you want something special to happen for class variables, since 
> reading from such a variable can theoretically provoke an action ?
> 
> - how do you want to handle class variables that have the same 
> properties but have unique instance numbers? For example,
> 
> A = handle(line(0,0));
> B = handle(line(0,0));
> 
> Should "A" and "B" compare the same or different?
> 
> - Case-sensitive string comparisons? Should it depend upon the variable?
> 
> - is blank padding significant for strings? Leading or trailing? Or 
> should trailing blanks not be significant for character arrays (since 
> all the rows must be the same length and a difference in some other row 
> might be forcing this row to be longer), or should trailing blanks be 
> significant for character arrays if blanks are detected at the end of 
> every row (which might imply deliberate allocation as a longer array)
> 
> - should unicode 8764 ("mathematical tilde operator") compare the same 
> as '~', should the unicode ellipsis character compare the same as '...', 
> should unicode m-dash and n-dash compare the same; and so on for other 
> strong glyph similarities?
> 
> - has Walter confused you enough yet?
> 
> -
0
Reply DLH 8/5/2010 3:44:04 PM

"DLH " <nospam@nospam.com> wrote in message <i3em84$jme$1@fred.mathworks.com>...
> Walter,
> 
> Thanks for your response, and no you have not confused me yet.
> 
> The points you raised are valid, and should be handled appropriately for a general algorithm.  For my immediate purposes I merely want to perform a case-insensitive search, listing the differences in the fields common to both (or more) structures while ignoring whitespace.  Field order is irrelevant, and although I expect all my structures to have the same fields the algorithm should detect and report any uncommon (extra or missing) fields.
> 
> Thanks.
> __
> dlh
> 
> Walter Roberson <roberson@hushmail.com> wrote in message <YcA6o.1205$1v3.582@newsfe20.iad>...
> > DLH wrote:
> > > Is there a way (command/s) to diff a structure similar to diff'ing files 
> > > on a unix/linux system?  I have a several structures with dozens of 
> > > fields, both strings & numbers, and I'd like a quick and simple way of 
> > > findings which fields are different and what those differences are.  Any 
> > > suggestions?
> > 
> > No such routine is provided with Matlab (or at least I've never seen one.)
> > 
> > A routine like this would not be all that difficult to write, but you 
> > would have to decide on a couple of things:
> > 
> > - breadth first search, or depth first search?
> > 
> > - if the field order is different but the same fields are there, then is 
> > this a "difference" that should be noted?
> > 
> > - do you want something special to happen for class variables, since 
> > reading from such a variable can theoretically provoke an action ?
> > 
> > - how do you want to handle class variables that have the same 
> > properties but have unique instance numbers? For example,
> > 
> > A = handle(line(0,0));
> > B = handle(line(0,0));
> > 
> > Should "A" and "B" compare the same or different?
> > 
> > - Case-sensitive string comparisons? Should it depend upon the variable?
> > 
> > - is blank padding significant for strings? Leading or trailing? Or 
> > should trailing blanks not be significant for character arrays (since 
> > all the rows must be the same length and a difference in some other row 
> > might be forcing this row to be longer), or should trailing blanks be 
> > significant for character arrays if blanks are detected at the end of 
> > every row (which might imply deliberate allocation as a longer array)
> > 
> > - should unicode 8764 ("mathematical tilde operator") compare the same 
> > as '~', should the unicode ellipsis character compare the same as '...', 
> > should unicode m-dash and n-dash compare the same; and so on for other 
> > strong glyph similarities?
> > 
> > - has Walter confused you enough yet?
> > 
> > -

A hint:
help fieldnames
help strcmpi

Oleg
0
Reply Oleg 8/5/2010 5:32:07 PM

"DLH " <nospam@nospam.com> wrote in message <i3em84$jme$1@fred.mathworks.com>...
> Walter,
> 
> Thanks for your response, and no you have not confused me yet.
> 
> The points you raised are valid, and should be handled appropriately for a general algorithm.  For my immediate purposes I merely want to perform a case-insensitive search, listing the differences in the fields common to both (or more) structures while ignoring whitespace.  Field order is irrelevant, and although I expect all my structures to have the same fields the algorithm should detect and report any uncommon (extra or missing) fields.
> 
> Thanks.

There is ambiguity if you decide to ignore fieldname case. If the same structure have two fields that differ by case, which one should be compared? There is no sense to allow such thing.

Otherwise here is a short function to compare structures (with case-sensitive then):

function strucdiff(s1, s2, s1str, s2str)
% Calling syntax: >> strucdiff(s1, s2)

if nargin<=2
    s1str = inputname(1);
    s2str = inputname(2);
end

if isstruct(s1) && isstruct(s2)
    f1 = fieldnames(s1);
    f2 = fieldnames(s2);
    g1 = setdiff(f2,f1);
    for k=1:length(g1)
        fprintf('<%s.%s> is missing.\n', s1str, g1{k});
    end
    g2 = setdiff(f1,f2);
    for k=1:length(g2)
        fprintf('<%s.%s> is missing.\n', s2str, g2{k});
    end
    fcommon = intersect(f1,f2);
    for k=1:length(fcommon)
        fk = fcommon{k};
        strucdiff(s1.(fk), s2.(fk), ...
                  [s1str '.' fk], [s2str '.' fk])
    end
elseif ~isstruct(s1) && isstruct(s2)
    fprintf('<%s> is not struct but <%s> is.\n', s1str, s2str);
elseif isstruct(s1) && ~isstruct(s2)
    fprintf('<%s> is struct but <%s> is not.\n', s1str, s2str);
elseif ~isequal(s1, s2)
    fprintf('<%s> and <%s> differs.\n', s1str, s2str);
end

end

% Bruno 
0
Reply Bruno 8/5/2010 6:17:20 PM

"DLH " <nospam@nospam.com> wrote in message <i3eg55$6bs$1@fred.mathworks.com>...
> Is there a way (command/s) to diff a structure similar to diff'ing files on a unix/linux system?  I have a several structures with dozens of fields, both strings & numbers, and I'd like a quick and simple way of findings which fields are different and what those differences are.  Any suggestions?
> 
> Thanks,
> dlh

a hint:
- this FEX submission will completely dissect a STRUCT
- the output struct contains information re
  the anatomy
  the content
- this makes it very easy to comapere STRUCTs
- see example below

http://www.mathworks.com/matlabcentral/fileexchange/1348

     clear s;     % <- save old stuff!
     s.a=pi;
     s.b.c=magic(3);
     s.d.e.f={struct('a','foo'),'goo',{magic(3)}};
     s.g(2,2)=s;
     [fa,fc,fs]=gettok(s,'-f',-3,'-Q');     % <- the call...
     disp(fs.field);     % <- == fa
%{
% note: see original version for correct formatting...
% col 1: field syntax (EVALuable)
% col 2: field content
    's.a'                         [    3.1416]
    's.b.c'                       [3x3 double]
    's.d.e.f{1,1}.a'              'foo'       
    's.d.e.f{1,2}'                'goo'       
    's.d.e.f{1,3}{1,1}'           [3x3 double]
    's.g(1,1).a'                            []
    's.g(1,1).b'                            []
    's.g(1,1).d'                            []
    's.g(2,1).a'                            []
    's.g(2,1).b'                            []
    's.g(2,1).d'                            []
    's.g(1,2).a'                            []
    's.g(1,2).b'                            []
    's.g(1,2).d'                            []
    's.g(2,2).a'                  [    3.1416]
    's.g(2,2).b.c'                [3x3 double]
    's.g(2,2).d.e.f{1,1}.a'       'foo'       
    's.g(2,2).d.e.f{1,2}'         'goo'       
    's.g(2,2).d.e.f{1,3}{1,1}'    [3x3 double]
%}

us
0
Reply us1 (8054) 8/5/2010 7:13:06 PM

5 Replies
256 Views

(page loaded in 0.913 seconds)


Reply: