function mergedStruct = addStructField(structOriginal, structNewElements)
% adds fields from a struct to another struct if not existent
% 
% mergedStruct = addStructField(structOriginal, structNewFields) adds the
%   fields of structNewFields to structOriginal if the fields are not yet
%   existent.
%
% See also: mergeStruct, struct

% MDT: MATLAB developement tools
%   Copyright (C) 2012 Alexander Reinhold
%
% This program is free software: you can redistribute it and/or modify it 
%   under the terms of the GNU Lesser General Public License as published 
%   by the Free Software Foundation, either version 3 of the License, or 
%   (at your option) any later version.
%
% This program is distributed in the hope that it will be useful, but 
%   WITHOUT ANY WARRANTY; without even the implied warranty of 
%   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
%   Lesser General Public License for more details.
%
% You should have received a copy of the GNU Lesser General Public License 
%   along with this program.  If not, see <http://www.gnu.org/licenses/>.

%% Check if inputs are structs
if ~isstruct(structOriginal)
        error(['Input ' 1 'is no struct!']);
end
if ~isstruct(structNewElements)
        error(['Input ' 2 'is no struct!']);
end

mergedStruct = structOriginal;

% Get field names and values
names = fieldnames(structNewElements);
values = struct2cell(structNewElements);
% Assign each
for iField = 1:length(names)
    if ~isfield(mergedStruct, names{iField})
        mergedStruct.(names{iField}) = values{iField};
    end
end

end
