View License

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

» Watch video

Highlights from
compare, alltrue

Join the 15-year community celebration.

Play games and win prizes!

» Learn more

3.7 | 3 ratings Rate this file 1 Download (last 30 days) File Size: 2.3 KB File ID: #18776 Version: 1.1

compare, alltrue


Dario (view profile)


15 Feb 2008 (Updated )

compares structs, cells and arrays element-by-element

| Watch this File

File Information

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.

MATLAB release MATLAB 7.3 (R2006b)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (5)
04 Feb 2010 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

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

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.');

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.

Comment only
19 Mar 2009 ygal

ygal (view profile)

Good job !

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.

Comment only
24 May 2009 1.1

fieldnames are now sorted, before they are compared

Contact us