Code covered by the BSD License  

Highlights from
compareStruct

compareStruct

by

 

01 Feb 2013 (Updated )

Compare Structs fields

compareStruct(a,b)
% compareStruct - Compare Structs fields
% Description : returns logical 1 (true) if the input structs have the same
% fields, and logical 0 (false) otherwise, no matter their content.
%
% Syntax:   = compareStruct(a,b)
%
% Inputs:
%    a - First Struct
%    b - Second Struct
%
% Outputs:
%        B - Boolean for the Struct type equality test
%
% Example: 
%    struct1.x = 1; struct1.y = 5;
%    struct2.x = 2; struct2.y = 10;
%    B = compareStruct(struct1,struct2)
%
% Other m-files required: exist, strcmp, numel, fieldnames
% Subfunctions: none
% MAT-files required: none
%
% See also: isequal, strcmp, regexp

% Author: Marco Borges, Ph.D. Student, Computer/Biomedical Engineer
% UFMG, PPGEE, Neurodinamica Lab, Brazil
% email address: marcoafborges@gmail.com
% Website: http://www.cpdee.ufmg.br/
% January 2013; Last revision: v2 2013-02-07

% Changelog:
%   v2 : Changes compare struct like Jos (10584) mention

%------------- BEGIN CODE --------------
function B = compareStruct(a,b)
    if isstruct(a) && isstruct(b)
        if isequal(a,b)
            B = true;
        else
            B = isequal(sort(fieldnames(a)), sort(fieldnames(b)));
        end
    else
        error('Input arguments must be Struct type!');
    end
end
%------------- END OF CODE --------------

Contact us