From: DLH on
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
From: Walter Roberson on
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?

-
From: DLH on
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(a)hushmail.com> wrote in message <YcA6o.1205$1v3.582(a)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?
>
> -
From: Oleg Komarov on
"DLH " <nospam(a)nospam.com> wrote in message <i3em84$jme$1(a)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(a)hushmail.com> wrote in message <YcA6o.1205$1v3.582(a)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
From: Bruno Luong on
"DLH " <nospam(a)nospam.com> wrote in message <i3em84$jme$1(a)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