view copyWithDefault.m @ 275:3ea2ae2a3d15 feature/beams

Improvments to eval on grids. Added function to extrac components of a vector gf.
author Jonatan Werpers <jonatan@werpers.com>
date Tue, 06 Sep 2016 17:28:11 +0200
parents 46256fffa329
children 499653b553b8
line wrap: on
line source

% Copy the struct src to dest with default values from default
%   dest = copyWithDefault(src, default)
function dest = copyWithDefault(src, default)
    % src does not have a value => use default
    if isempty(src)
        dest = default;
        return
    end

    % src has a value and is not a struct => use src
    if ~isstruct(src)
        dest = src;
        return
    end


    % src has a value and is a struct => add all default fields
    dest = src;

    fn = fieldnames(default);
    for i = 1:length(fn)
        if isfield(src, fn{i})
            srcField = src.(fn{i});
        else
            srcField = [];
        end

        dest.(fn{i}) = copyWithDefault(srcField, default.(fn{i}));
    end
end