source: trunk/src/@xmltree/copy.m @ 1099

Last change on this file since 1099 was 925, checked in by sommeria, 9 years ago

xmltree updated

File size: 1.6 KB
RevLine 
[925]1function tree = copy(tree,subuid,uid)
2% XMLTREE/COPY Copy Method (copy a subtree in another branch)
3% FORMAT tree = copy(tree,subuid,uid)
4%
5% tree      - XMLTree object
6% subuid    - UID of the subtree to copy
7% uid       - UID of the element where the subtree must be duplicated
8%__________________________________________________________________________
9%
10% Copy a subtree to another branch.
11% The tree parameter must be in input AND in output.
12%__________________________________________________________________________
13% Copyright (C) 2002-2015  http://www.artefact.tk/
14
15% Guillaume Flandin
16% $Id: copy.m 6480 2015-06-13 01:08:30Z guillaume $
17
18
19%error(nargchk(2,3,nargin));
20
21if nargin == 2
22    uid = parent(tree,subuid);
23end
24
25l = length(tree);
26tree = sub_copy(tree,subuid,uid);
27tree.tree{uid}.contents = [tree.tree{uid}.contents l+1];
28
29% to have the copy next to the original and not at the end?
30%  contents = get(tree,parent,'contents');
31%  i = find(contents==uid);
32%  tree = set(tree,parent,'contents',[contents(1:i) l+1 contents(i+1:end)]);
33
34%==========================================================================
35function tree = sub_copy(tree,uid,p)
36
37    l = length(tree);
38    tree.tree{l+1} = tree.tree{uid};
39    tree.tree{l+1}.uid = l+1;
40    tree.tree{l+1}.parent = p;
41    tree.tree{l+1}.contents = [];
42    if isfield(tree.tree{uid},'contents')
43        contents = get(tree,uid,'contents');
44        m = length(tree);
45        for i=1:length(contents)
46            tree.tree{l+1}.contents = [tree.tree{l+1}.contents m+1];
47            tree = sub_copy(tree,contents(i),l+1);
48            m = length(tree);
49        end
50    end
Note: See TracBrowser for help on using the repository browser.