# compare, alltrue

Version 1.1.0.0 (2.3 KB) by
compares structs, cells and arrays element-by-element
Updated 24 May 2009

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.

### Cite As

Dario (2024). compare, alltrue (https://www.mathworks.com/matlabcentral/fileexchange/18776-compare-alltrue), MATLAB Central File Exchange. Retrieved .

##### MATLAB Release Compatibility
Created with R2006b
Compatible with any release
##### Platform Compatibility
Windows macOS Linux
##### Categories
Find more on Structures in Help Center and MATLAB Answers