Skip to content

Commit

Permalink
Fixed merging bug - Imports are now merged deeply.
Browse files Browse the repository at this point in the history
  • Loading branch information
ptomask committed Sep 16, 2011
1 parent 964d8cb commit 924b2c8
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
14 changes: 12 additions & 2 deletions merge_struct.m
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,19 @@
% fields of p and s. If there are equal field names in p and s, fields in p
% are overwriten with their peers from s.
%
function result = merge_struct(p, s, donotmerge)
function result = merge_struct(p, s, donotmerge, deep)
if ~( isstruct(p) && isstruct(s) )
error('Only structures can be merged.');
end;
if ~exist('donotmerge','var')
donotmerge = {};
end
if ~exist('deep','var')
deep = 0;
elseif strcmp(deep, 'deep')
deep = 1;
end;

result = p;
for i = fields(s)'
fld = char(i);
Expand All @@ -26,6 +32,10 @@
%disp(['fieldname: ',fld]);
%disp(s.(fld));
%disp('----------');
result.(fld) = s.(fld);
if deep == 1 && isfield(result, fld)
result.(fld) = merge_struct(result.(fld), s.(fld), donotmerge, deep);
else
result.(fld) = s.(fld);
end;
end;
end
4 changes: 2 additions & 2 deletions mergeimports.m
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@
end;
end;
for i = 1:length(collected_imports)
result = merge_struct(result, collected_imports{i});
result = merge_struct(result, collected_imports{i}, {}, 'deep');
end;
if any(verb == 1); fprintf([indent,'} struct\n']); end; % for debugging
end
Expand All @@ -101,7 +101,7 @@
collected_nonstruct = {};
for i = 1:length(data)
if isstruct(data{i})
merged_structs = merge_struct(merged_structs, data{i});
merged_structs = merge_struct(merged_structs, data{i}, {}, 'deep');
else
collected_nonstruct{end+1} = data{i};
end;
Expand Down

0 comments on commit 924b2c8

Please sign in to comment.