-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathremove_child_lines.m
61 lines (58 loc) · 2.09 KB
/
remove_child_lines.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
49
50
51
52
53
54
55
56
57
58
59
60
61
function lines = remove_child_lines(lines, varargin)
% REMOVE_CHILD_LINES Removes lines when their parent line is in the list.
% (Also removes if the parent's parent is in the list and so on).
%
% Inputs:
% lines Vector of Simulink lines.
% varargin{1} Mode: 'IfParentExists' or 'All'
% (Default) 'IfParentExists' - remove line if any parent is
% found recursively (same functionality described above).
% 'All' - remove any lines that have a parent.
%
% Outputs:
% lines Vector of Simulink lines. Same as input, but with appropriate
% lines removed.
if ~isempty(varargin)
mode = lower(varargin{1});
else
mode = 'all';
end
switch mode
case 'ifparentexists'
for i = length(lines):-1:1
if ancestor_in_list(lines(i), lines)
% An ancestor of the line is already in the list.
lines(i) = []; % Remove line
end
end
case 'all'
for i = length(lines):-1:1
if get_param(lines(i), 'LineParent') ~= -1
% There is a parent
lines(i) = []; % Remove line
end
end
otherwise
error('Unexpected value for optional argument. Expecting ''IfParentExists'' or ''All''.')
end
end
function bool = ancestor_in_list(line, lineArray)
% Recursively looks for a parent of line in the lineArray.
% line - a line
% lineArray - a vector of lines
parentLn = get_param(line, 'LineParent');
if parentLn ~= -1
% There is a parent.
if isempty(find(parentLn == lineArray, 1))
% Parent not in list.
% Check if another ancestor is in list.
bool = ancestor_in_list(parentLn, lineArray);
else
% Parent in list.
bool = true;
end
else
% There is no parent to begin with.
bool = false;
end
end