-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcombineStructs.m
42 lines (40 loc) · 1.06 KB
/
combineStructs.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
% sc = combineStructs(s1,s2)
%
% combine two structures, if the input structures have common fields, then
% the field values of the first structure will be overwritten with the
% values of the second structure and a warning that this happened will
% be displayed
%
% in:
% s1 - first structure
% s2 - second structure
% out:
% sc - combined structure containing all fields from s1 and s2
function sc = combineStructs(s1,s2)
% this makes sure that the for-loop below is over the smaller
% number of fields
fnames = fieldnames(s1);
num = 1;
len = length(fnames);
fnames = fieldnames(s2);
if len>=length(fnames)
len = length(fnames);
sc = s1;
else
num = 2;
fnames = fieldnames(s1);
sc = s2;
s2 = s1;
s1 = sc;
end
for i=1:len
if isfield(s1,fnames{i})
warning('tools:structOverwrite','overwriting s1.%s with s2.%s',...
fnames{i},fnames{i})
if num==1
sc.(fnames{i}) = s2.(fnames{i});
end
else
sc.(fnames{i}) = s2.(fnames{i});
end
end