Skip to content

Commit f5c47ba

Browse files
committed
refactor set defaults
1 parent 05f497b commit f5c47ba

File tree

3 files changed

+84
-42
lines changed

3 files changed

+84
-42
lines changed

src/utils/setDefaults.m

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
function structure = setDefaults(structure, fieldsToSet)
2+
% structure = setDefaults(structure, fieldsToSet)
3+
%
4+
% recursively loop through the fields of a structure and sets a value if they don't exist
5+
%
6+
7+
fieldsToSet = orderfields(fieldsToSet);
8+
9+
names = fieldnames(fieldsToSet);
10+
11+
for i = 1:numel(names)
12+
13+
thisField = fieldsToSet.(names{i});
14+
15+
if isfield(structure, names{i}) && isstruct(structure.(names{i}))
16+
17+
structure.(names{i}) = ...
18+
setDefaults(...
19+
structure.(names{i}), ...
20+
fieldsToSet.(names{i})...
21+
);
22+
23+
else
24+
25+
structure = setFieldToIfNotPresent( ...
26+
structure, ...
27+
names{i}, ...
28+
thisField);
29+
end
30+
31+
end
32+
33+
structure = orderfields(structure);
34+
35+
end
36+
37+
function structure = setFieldToIfNotPresent(structure, fieldName, value)
38+
if ~isfield(structure, fieldName)
39+
structure.(fieldName) = value;
40+
end
41+
end

src/utils/setDefaultsPTB.m

Lines changed: 0 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -84,45 +84,3 @@
8484
cfg = orderfields(cfg);
8585

8686
end
87-
88-
function structure = setDefaults(structure, fieldsToSet)
89-
% structure = setDefaultFields(structure, fieldsToSet)
90-
%
91-
% recursively loop through the fields of a structure and sets a value if they don't exist
92-
%
93-
94-
fieldsToSet = orderfields(fieldsToSet);
95-
96-
names = fieldnames(fieldsToSet);
97-
98-
for i = 1:numel(names)
99-
100-
thisField = fieldsToSet.(names{i});
101-
102-
if isfield(structure, names{i}) && isstruct(structure.(names{i}))
103-
104-
structure.(names{i}) = ...
105-
setDefaults(...
106-
structure.(names{i}), ...
107-
fieldsToSet.(names{i})...
108-
);
109-
110-
else
111-
112-
structure = setFieldToIfNotPresent( ...
113-
structure, ...
114-
names{i}, ...
115-
thisField);
116-
end
117-
118-
end
119-
120-
structure = orderfields(structure);
121-
122-
end
123-
124-
function structure = setFieldToIfNotPresent(structure, fieldName, value)
125-
if ~isfield(structure, fieldName)
126-
structure.(fieldName) = value;
127-
end
128-
end

tests/test_setDefaults.m

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
function test_suite = test_setDefaults %#ok<*STOUT>
2+
try % assignment of 'localfunctions' is necessary in Matlab >= 2016
3+
test_functions = localfunctions(); %#ok<*NASGU>
4+
catch % no problem; early Matlab versions can use initTestSuite fine
5+
end
6+
initTestSuite;
7+
end
8+
9+
function test_setDefaultsWrite()
10+
11+
%% set up
12+
structure = struct();
13+
14+
fieldsToSet.field = 1;
15+
16+
structure = setDefaults(structure, fieldsToSet);
17+
18+
%% data to test against
19+
expectedStructure.field = 1;
20+
21+
%% test
22+
assertEqual(expectedStructure, structure);
23+
24+
end
25+
26+
function test_setDefaultsNoOverwrite()
27+
28+
% set up
29+
structure.field.subfield_1 = 3;
30+
31+
fieldsToSet.field.subfield_1 = 1;
32+
fieldsToSet.field.subfield_2 = 1;
33+
34+
structure = setDefaults(structure, fieldsToSet);
35+
36+
% data to test against
37+
expectedStructure.field.subfield_1 = 3;
38+
expectedStructure.field.subfield_2 = 1;
39+
40+
% test
41+
assert(isequal(expectedStructure, structure));
42+
43+
end

0 commit comments

Comments
 (0)