File Exchange

image thumbnail

compare, alltrue

version 1.1 (2.3 KB) by

compares structs, cells and arrays element-by-element

3 Ratings



View License

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.

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

% >> 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

% >> 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.

Comments and Ratings (5)

David Groppe

David Groppe (view profile)

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

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

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;
error('Numeric and logical arrays must have the same size.');

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.


ygal (view profile)

Good job !

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.



fieldnames are now sorted, before they are compared

MATLAB Release
MATLAB 7.3 (R2006b)

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video

Win prizes and improve your MATLAB skills

Play today