function isstructequal = cmpstruct(struct1,struct2)
% compares two structures
isstructequal=0;
Fields = fieldnames(struct1);
for i=1:length(Fields)
    if isfield(struct2,Fields{i})
        try
            if ischar(struct1.(Fields{i}))
                % string
                if ~strcmp(struct1.(Fields{i}),struct2.(Fields{i}))
                    return
                end
            elseif iscell(struct1.(Fields{i}))
                if any(size(struct1.(Fields{i}))-size(struct2.(Fields{i})))
                    return
                else
                    for n=1:length(struct1.(Fields{i}))
                        if any(size(struct1.(Fields{i}){n})-size(struct2.(Fields{i}){n}))
                            return
                        end
                        if any(any(abs(struct1.(Fields{i}){n}-struct2.(Fields{i}){n})>10^-13))
                            return
                        end
                    end
                end
            elseif isstruct(struct1.(Fields{i}))     % structure with numeric fields
                struct1_i=struct1.(Fields{i});
                struct2_i=struct2.(Fields{i});
                Fields_struct_i = fieldnames(struct1_i);
                Fields_struct2_i = fieldnames(struct2_i);
                if length(Fields_struct_i)~=length(Fields_struct2_i)
                    return
                end
                for n=1:length(Fields_struct_i)
                    if any(size(struct1_i.(Fields_struct_i{n}))-size(struct2_i.(Fields_struct_i{n})))
                        return
                    else
                        if any(abs(struct1_i.(Fields_struct_i{n})-struct2_i.(Fields_struct_i{n}))>10^-13)
                            return
                        end
                    end
                end
            else
                % number
                %if ~isequal(struct1.(Fields{i}),struct2.(Fields{i}))
                if any(size(struct1.(Fields{i}))-size(struct2.(Fields{i})))
                    return
                else
                    if any(abs(struct1.(Fields{i})-struct2.(Fields{i}))>10^-13)
                        return
                    end
                end
            end
        catch
            disp('Failure in cmpstruct');
            return
        end
    else
        return
    end
end
isstructequal=1;   % structures are equal