-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathtgDuplicateTier.m
More file actions
73 lines (58 loc) · 1.97 KB
/
Copy pathtgDuplicateTier.m
File metadata and controls
73 lines (58 loc) · 1.97 KB
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
function tgNew = tgDuplicateTier(tg, originalInd, newInd, newTierName)
% function tgNew = tgDuplicateTier(tg, originalInd, newInd, newTierName)
%
% Duplicates tier originalInd to new tier with specified index newInd
% (existing tiers are shifted).
% It is highly recommended to set a name to the new tier (this can also be done
% later by tg.setTierName). Otherwise, both original and new tiers have the
% same name which is permitted but not recommended. In such a case, we
% cannot use the comfort of using tier name instead of its index in other
% functions.
%
% tg ... TextGrid object
% originalInd ... tier index or 'name'
% newInd ... [optional] new tier index (1 = the first, Inf = the last [default])
% newTierName ... [optional but recommended] name of the new tier
%
% v1.0, Tomas Boril, borilt@gmail.com
%
% tg = tgRead('demo/H.TextGrid');
% tg2 = tgDuplicateTier(tg, 'word', 1, 'NEW');
% tg2 = tgDuplicateTier(tg2, 'word', Inf, 'NEW');
% tgPlot(tg2);
if nargin < 2 || nargin > 4
error('Wrong number of arguments.')
end
ntiers = tgGetNumberOfTiers(tg);
if nargin == 2
newInd = Inf;
end
if isinf(newInd)
if newInd > 0
newInd = ntiers+1;
else
error('newInd must be integer >= 1 or +Inf')
end
end
originalInd = tgI(tg, originalInd);
if nargin == 2 || nargin == 3
newTierName = tg.tier{originalInd}.name;
end
if ~isInt(newInd)
error(['newInd must be integer >= 1 or +Inf [' num2str(newInd) ']']);
end
if newInd < 1 || newInd>ntiers+1
error(['newInd out of range [1; ntiers+1], newInd = ' num2str(newInd) ', ntiers = ' num2str(ntiers)]);
end
for I = 1: ntiers
if strcmp(tg.tier{I}.name, newTierName)
warning(['TextGrid has a duplicate tier name [', newTierName, ']. You should not use the name for indexing to avoid ambiguity.']);
end
end
tgNew = tg;
tOrig = tg.tier{originalInd};
for I = ntiers + 1: -1: newInd+1
tgNew.tier{I} = tgNew.tier{I-1};
end
tgNew.tier{newInd} = tOrig;
tgNew.tier{newInd}.name = newTierName;