-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathcopy.m
48 lines (42 loc) · 1.61 KB
/
copy.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
43
44
45
46
47
48
function tree = copy(tree,subuid,uid)
% XMLTREE/COPY Copy Method (copy a subtree in another branch)
% FORMAT tree = copy(tree,subuid,uid)
%
% tree - XMLTree object
% subuid - UID of the subtree to copy
% uid - UID of the element where the subtree must be duplicated
%_______________________________________________________________________
%
% Copy a subtree to another branch
% The tree parameter must be in input AND in output
%_______________________________________________________________________
% Copyright (C) 2002-2008 http://www.artefact.tk/
% Guillaume Flandin <guillaume@artefact.tk>
% $Id: copy.m 1460 2008-04-21 17:43:18Z guillaume $
error(nargchk(2,3,nargin));
if nargin == 2
uid = parent(tree,subuid);
end
l = length(tree);
tree = sub_copy(tree,subuid,uid);
tree.tree{uid}.contents = [tree.tree{uid}.contents l+1];
% to have the copy next to the original and not at the end?
% contents = get(tree,parent,'contents');
% i = find(contents==uid);
% tree = set(tree,parent,'contents',[contents(1:i) l+1 contents(i+1:end)]);
%=======================================================================
function tree = sub_copy(tree,uid,p)
l = length(tree);
tree.tree{l+1} = tree.tree{uid};
tree.tree{l+1}.uid = l+1;
tree.tree{l+1}.parent = p;
tree.tree{l+1}.contents = [];
if isfield(tree.tree{uid},'contents')
contents = get(tree,uid,'contents');
m = length(tree);
for i=1:length(contents)
tree.tree{l+1}.contents = [tree.tree{l+1}.contents m+1];
tree = sub_copy(tree,contents(i),l+1);
m = length(tree);
end
end