Mercurial > repos > public > sbplib
comparison copyWithDefault.m @ 240:46256fffa329 feature/beams
Added some stuff for default structs. Improved interface for beam.
author | Jonatan Werpers <jonatan@werpers.com> |
---|---|
date | Wed, 03 Aug 2016 12:30:27 +0200 |
parents | |
children | 499653b553b8 |
comparison
equal
deleted
inserted
replaced
239:ba56e0d621f2 | 240:46256fffa329 |
---|---|
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 if ~isstruct(src) | |
12 dest = src; | |
13 return | |
14 end | |
15 | |
16 | |
17 % src has a value and is a struct => add all default fields | |
18 dest = src; | |
19 | |
20 fn = fieldnames(default); | |
21 for i = 1:length(fn) | |
22 if isfield(src, fn{i}) | |
23 srcField = src.(fn{i}); | |
24 else | |
25 srcField = []; | |
26 end | |
27 | |
28 dest.(fn{i}) = copyWithDefault(srcField, default.(fn{i})); | |
29 end | |
30 end |