File: AreStructsEqualOnFields.m

package info (click to toggle)
psychtoolbox-3 3.0.19.14.dfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 86,796 kB
  • sloc: ansic: 176,245; cpp: 20,103; objc: 5,393; sh: 2,753; python: 1,397; php: 384; makefile: 193; java: 113
file content (36 lines) | stat: -rw-r--r-- 1,154 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
function areEqual = AreStructsEqualOnFields(struct1,struct2,theFields)
% areEqual = AreStructsEqualOnFields(struct1,struct2,theFields)
%
% Returns 1 if two structs share the same value on each of the passed
% fields, 0 otherwise.  The equality of each field in the two structs is
% checked with a call to isequal()
%
% 5/1/05     dhb, jmk   Handle cell and struct fields, a little bit.
% 2012/06/12 DN         Can now handle fields of any data type supported by
%                       isequal(). Added some input checks


psychassert(isscalar(struct1) && isscalar(struct2),'structs must be scalar');

if ~iscell(theFields)
    theFields = {theFields};
end

nFields = length(theFields);
areEqual = 0;

% Loop over fields.  Return on any indicator of non-equality.
% If we make it out the bottom, then set return value to 1
for i = 1:nFields
    % If either struct is missing the passed field, we say not equal.
    if ~isfield(struct1,theFields{i}) || ~isfield(struct2,theFields{i})
        return;
    end
    
    if ~isequal(struct1.(theFields{i}),struct2.(theFields{i}))
        return;
    end
end

% If we arrive here, they are equal
areEqual = 1;