Code covered by the BSD License  

Highlights from
compare, alltrue

3.66667

3.7 | 3 ratings Rate this file 2 Downloads (last 30 days) File Size: 2.3 KB File ID: #18776

compare, alltrue

by Dario

 

15 Feb 2008 (Updated 24 May 2009)

compares structs, cells and arrays element-by-element

| Watch this File

File Information
Description

the 'compare' function is useful if you have two objects of the same type and same size, in case of structs, also the fields must be the same.
this is not useful to look for the differences in the data structures, it just throws an error if the structure doesn't fit.

% COMPARE(A,B) compares A and B element-by-element

% A and B must be of the same type, and of one of the following types:
% struct, cell array, numeric or logical array or string.
% A and B may contain further elements of the types listed above (if they
% are cell arrays or structs).
% If A and B are structs, and/or contain structs, all structs the must have
% the the same fields.
% If A and B are arrays, and/or contain arrays, all arrays must be of the
% same size.
% Strings don't need to be of the same length.
% The return value C is of the same type as A and B, and contains the same
% elements as A and B, but C contains only logical values, in place of any
% numeric values and strings in A and B.
% If a number or string in A equals the number (resp. string) in B, C has a
% logical 1 in the same place as the number (resp. string). Otherwise C has
% a logical 0 in this place.

% HINT:
% use alltrue(compare(a,b)) to check if a and b are identical

% EXAMPLE 1:
% >> a = {'a',1,[1,1]};
% >> b = {'a',1,[1,2]};
% >> a == b
% ??? Undefined function or method 'eq' for input arguments of type 'cell'.
% >> compare(a,b)
% ans =
% [1] [1] [1x2 logical]
% >> ans{1,3}
% ans =
% 1 0

% EXAMPLE 2:
% >> a.f1 = 'foo';
% >> a.f2 = 'bar';
% >> b.f1 = 'foo';
% >> b.f2 = 'rab';
% >> compare(a,b)
% ans =
% f1: 1
% f2: 0

% ALLTRUE(A) detects if A contains only true values.
% A can be a combination of structs, cell arrays, numeric and logical
% arrays and strings. If A contains any 0 or '', ISTRUE(A) return 0,
% otherwise 1 is returned.

MATLAB release MATLAB 7.3 (R2006b)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (5)
29 May 2008 Pepijn Boer

Works fine by me, but you should sort the fieldnames of a struct first, before comparing. Thus line 47 and 48 should become:
       Afields = sort(fieldnames(A));
       Bfields = sort(fieldnames(B));

Otherwise the order of the fields will be an issue, and it should be about the content.

19 Mar 2009 ygal

Good job !

05 Sep 2009 naveen kumar B T

hello, good day.
i m using 7.0.1 version.
i kept both compare.m and alltrue.m in workspace
i entered commands specified by you as below and got this result.
>> a.f1 = 'foo';
>> a.f2 = 'bar';
>> b.f1 = 'foo';
>> b.f2 = 'rab';
>> compare(a,b)
??? Too many inputs.

Error in ==> compare at 59
           C = cellfun(@(a,b)compare(a,b),A,B,'UniformOutput',false);

Error in ==> compare at 49
       if alltrue(compare(Afields,Bfields))

PLEASE FIX IT. thanks for your work. i welcome the reply.

27 Jan 2010 Jose Benavides

Adding the following code starting at line 51 fixed it for me. It was a matter of adding an extra for loop to handle the struct arrays.

strsize = length(A);
for i=1:size(Afields,1)
   field = Afields{i};
   for j=1:strsize
       eval(['C(j).',field,'=strcompare(A(j).',field,',B(j).',field,');']);
   end
end

Also helpful for me was adding the following code to check for equivalent nan values (the eq operation returns a 0 for nan's).

elseif isnumeric(A) || islogical(A)
   if size(A)==size(B)
       C = A==B;
       AN = isnan(A);
       BN = isnan(B);
       NN = AN & BN;
       C = C | NN;
   else
       error('Numeric and logical arrays must have the same size.');
   end

04 Feb 2010 David Groppe

Got the following error when trying to compare a struct variable to itself. Clearly some bugs still need to be worked out:

>> compare(GND.chanlocs,GND.chanlocs);
??? Error using ==> compare
Too many input arguments.

Error in ==> compare at 52
               eval(['C.',field,'=compare(A.',field,',B.',field,');']);

Please login to add a comment or rating.
Updates
24 May 2009

fieldnames are now sorted, before they are compared

Tag Activity for this File
Tag Applied By Date/Time
compare Dario 22 Oct 2008 09:47:52
cell Dario 22 Oct 2008 09:47:52
struct Dario 22 Oct 2008 09:47:52
string Dario 22 Oct 2008 09:47:52
compare Georg Wiora 17 Dec 2008 05:09:32
compare ygal 06 Mar 2009 05:31:11
compare Patricia 20 Mar 2009 06:12:59
array Dario 26 May 2009 11:32:29

Contact us at files@mathworks.com