MERGESTRUCT - Merge structures with unique fields.

Contents

Syntax

   sout = MERGESTRUCT(varargin);

See also

Ressembles: CATSTRUCT, STRUCTA2ASTRUCT. Requires: STRUCT2CELL, CELL2STRUCT, UNIQUE.

Function implementation

function sout = mergestruct(varargin)

start with collecting fieldnames, checking implicitly that inputs are structures.

fn = [];
for k = 1:nargin
    try
        fn = [fn ; fieldnames(varargin{k})];                           %#ok
    catch MEstruct
        throw(MEstruct)
    end
end

make sure the field names are unique.

if length(fn) ~= length(unique(fn))
    error('mergestruct:FieldsNotUnique',...
        'Field names must be unique');
end

now concatenate the data from each struct. Can't use structfun since input structs may not be scalar.

c = [];
for k = 1:nargin
    try
        c = [c , struct2cell(varargin{k})];                            %#ok
    catch MEdata
        throw(MEdata);
    end
end

construct the output.

sout = cell2struct(c, fn, 1);
end % end of mergestruct