comparison copyWithDefault.m @ 820:501750fbbfdb

Merge with feature/grids
author Jonatan Werpers <jonatan@werpers.com>
date Fri, 07 Sep 2018 14:40:58 +0200
parents 499653b553b8
children
comparison
equal deleted inserted replaced
819:fdf0ef9150f4 820:501750fbbfdb
1 % Copy the struct src to dest with default values from default
2 % dest = copyWithDefault(src, default)
3 function dest = copyWithDefault(src, default)
4 % src does not have a value => use default
5 if isempty(src)
6 dest = default;
7 return
8 end
9
10 % src has a value and is not a struct => use src
11 % src has a value and default is not a struct => use src
12 if ~isstruct(src) || ~isstruct(default)
13 dest = src;
14 return
15 end
16
17
18 % src has a value and is a struct => add all default fields
19 dest = src;
20
21 fn = fieldnames(default);
22 for i = 1:length(fn)
23 if isfield(src, fn{i})
24 srcField = src.(fn{i});
25 else
26 srcField = [];
27 end
28
29 dest.(fn{i}) = copyWithDefault(srcField, default.(fn{i}));
30 end
31 end