annotate scripts/plot/ancestor.m @ 6851:05f6f120a65f

[project @ 2007-08-31 17:58:00 by jwe]
author jwe
date Fri, 31 Aug 2007 17:58:00 +0000
parents
children 93c65f2a5668
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
6851
05f6f120a65f [project @ 2007-08-31 17:58:00 by jwe]
jwe
parents:
diff changeset
1 ## Copyright (C) 2007 Michael Goffioul
05f6f120a65f [project @ 2007-08-31 17:58:00 by jwe]
jwe
parents:
diff changeset
2 ##
05f6f120a65f [project @ 2007-08-31 17:58:00 by jwe]
jwe
parents:
diff changeset
3 ## This program is free software; you can redistribute it and/or modify
05f6f120a65f [project @ 2007-08-31 17:58:00 by jwe]
jwe
parents:
diff changeset
4 ## it under the terms of the GNU General Public License as published by
05f6f120a65f [project @ 2007-08-31 17:58:00 by jwe]
jwe
parents:
diff changeset
5 ## the Free Software Foundation; either version 2 of the License, or
05f6f120a65f [project @ 2007-08-31 17:58:00 by jwe]
jwe
parents:
diff changeset
6 ## (at your option) any later version.
05f6f120a65f [project @ 2007-08-31 17:58:00 by jwe]
jwe
parents:
diff changeset
7 ##
05f6f120a65f [project @ 2007-08-31 17:58:00 by jwe]
jwe
parents:
diff changeset
8 ## This program is distributed in the hope that it will be useful,
05f6f120a65f [project @ 2007-08-31 17:58:00 by jwe]
jwe
parents:
diff changeset
9 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
05f6f120a65f [project @ 2007-08-31 17:58:00 by jwe]
jwe
parents:
diff changeset
10 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
05f6f120a65f [project @ 2007-08-31 17:58:00 by jwe]
jwe
parents:
diff changeset
11 ## GNU General Public License for more details.
05f6f120a65f [project @ 2007-08-31 17:58:00 by jwe]
jwe
parents:
diff changeset
12 ##
05f6f120a65f [project @ 2007-08-31 17:58:00 by jwe]
jwe
parents:
diff changeset
13 ## You should have received a copy of the GNU General Public License
05f6f120a65f [project @ 2007-08-31 17:58:00 by jwe]
jwe
parents:
diff changeset
14 ## along with this program; if not, write to the Free Software
05f6f120a65f [project @ 2007-08-31 17:58:00 by jwe]
jwe
parents:
diff changeset
15 ## Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
05f6f120a65f [project @ 2007-08-31 17:58:00 by jwe]
jwe
parents:
diff changeset
16 ## 02110-1301 USA
05f6f120a65f [project @ 2007-08-31 17:58:00 by jwe]
jwe
parents:
diff changeset
17
05f6f120a65f [project @ 2007-08-31 17:58:00 by jwe]
jwe
parents:
diff changeset
18 ## -*- texinfo -*-
05f6f120a65f [project @ 2007-08-31 17:58:00 by jwe]
jwe
parents:
diff changeset
19 ## @deftypefn {Function File} {@var{parent} =} ancestor (@var{h}, @var{type})
05f6f120a65f [project @ 2007-08-31 17:58:00 by jwe]
jwe
parents:
diff changeset
20 ## @deftypefnx {Function File} {@var{parent} =} ancestor (@var{h}, @var{type}, 'toplevel')
05f6f120a65f [project @ 2007-08-31 17:58:00 by jwe]
jwe
parents:
diff changeset
21 ## Return the first ancestor of handle object @var{h} whose type matches
05f6f120a65f [project @ 2007-08-31 17:58:00 by jwe]
jwe
parents:
diff changeset
22 ## @var{type}, where @var{type} is a character string. If @var{type} is a
05f6f120a65f [project @ 2007-08-31 17:58:00 by jwe]
jwe
parents:
diff changeset
23 ## cell array of strings, return the first parent whose type matches
05f6f120a65f [project @ 2007-08-31 17:58:00 by jwe]
jwe
parents:
diff changeset
24 ## any of the given type strings.
05f6f120a65f [project @ 2007-08-31 17:58:00 by jwe]
jwe
parents:
diff changeset
25 ##
05f6f120a65f [project @ 2007-08-31 17:58:00 by jwe]
jwe
parents:
diff changeset
26 ## If the handle object @var{h} is of type @var{type}, return @var{h}.
05f6f120a65f [project @ 2007-08-31 17:58:00 by jwe]
jwe
parents:
diff changeset
27 ##
05f6f120a65f [project @ 2007-08-31 17:58:00 by jwe]
jwe
parents:
diff changeset
28 ## If @code{"toplevel"} is given as a 3rd argument, return the highest
05f6f120a65f [project @ 2007-08-31 17:58:00 by jwe]
jwe
parents:
diff changeset
29 ## parent in the object hierarchy that matches the condition, instead
05f6f120a65f [project @ 2007-08-31 17:58:00 by jwe]
jwe
parents:
diff changeset
30 ## of the first (nearest) one.
05f6f120a65f [project @ 2007-08-31 17:58:00 by jwe]
jwe
parents:
diff changeset
31 ## @seealso{get, set}
05f6f120a65f [project @ 2007-08-31 17:58:00 by jwe]
jwe
parents:
diff changeset
32 ## @end deftypefn
05f6f120a65f [project @ 2007-08-31 17:58:00 by jwe]
jwe
parents:
diff changeset
33
05f6f120a65f [project @ 2007-08-31 17:58:00 by jwe]
jwe
parents:
diff changeset
34 function p = ancestor (h, type, toplevel)
05f6f120a65f [project @ 2007-08-31 17:58:00 by jwe]
jwe
parents:
diff changeset
35
05f6f120a65f [project @ 2007-08-31 17:58:00 by jwe]
jwe
parents:
diff changeset
36 if (nargin == 2 || nargin == 3)
05f6f120a65f [project @ 2007-08-31 17:58:00 by jwe]
jwe
parents:
diff changeset
37 p = [];
05f6f120a65f [project @ 2007-08-31 17:58:00 by jwe]
jwe
parents:
diff changeset
38 if (ischar (type))
05f6f120a65f [project @ 2007-08-31 17:58:00 by jwe]
jwe
parents:
diff changeset
39 type = { type };
05f6f120a65f [project @ 2007-08-31 17:58:00 by jwe]
jwe
parents:
diff changeset
40 endif
05f6f120a65f [project @ 2007-08-31 17:58:00 by jwe]
jwe
parents:
diff changeset
41 if (iscellstr (type))
05f6f120a65f [project @ 2007-08-31 17:58:00 by jwe]
jwe
parents:
diff changeset
42 look_first = true;
05f6f120a65f [project @ 2007-08-31 17:58:00 by jwe]
jwe
parents:
diff changeset
43 if (nargin == 3)
05f6f120a65f [project @ 2007-08-31 17:58:00 by jwe]
jwe
parents:
diff changeset
44 if (ischar (toplevel) && strcmp (toplevel, "toplevel"))
05f6f120a65f [project @ 2007-08-31 17:58:00 by jwe]
jwe
parents:
diff changeset
45 look_first = false;
05f6f120a65f [project @ 2007-08-31 17:58:00 by jwe]
jwe
parents:
diff changeset
46 else
05f6f120a65f [project @ 2007-08-31 17:58:00 by jwe]
jwe
parents:
diff changeset
47 error ("ancestor: third argument must be \"toplevel\"");
05f6f120a65f [project @ 2007-08-31 17:58:00 by jwe]
jwe
parents:
diff changeset
48 endif
05f6f120a65f [project @ 2007-08-31 17:58:00 by jwe]
jwe
parents:
diff changeset
49 endif
05f6f120a65f [project @ 2007-08-31 17:58:00 by jwe]
jwe
parents:
diff changeset
50 while (true)
05f6f120a65f [project @ 2007-08-31 17:58:00 by jwe]
jwe
parents:
diff changeset
51 if (isempty (h) || ! ishandle (h))
05f6f120a65f [project @ 2007-08-31 17:58:00 by jwe]
jwe
parents:
diff changeset
52 break;
05f6f120a65f [project @ 2007-08-31 17:58:00 by jwe]
jwe
parents:
diff changeset
53 endif
05f6f120a65f [project @ 2007-08-31 17:58:00 by jwe]
jwe
parents:
diff changeset
54 if (any (strcmpi (get (h, "type"), type)))
05f6f120a65f [project @ 2007-08-31 17:58:00 by jwe]
jwe
parents:
diff changeset
55 p = h;
05f6f120a65f [project @ 2007-08-31 17:58:00 by jwe]
jwe
parents:
diff changeset
56 if (look_first)
05f6f120a65f [project @ 2007-08-31 17:58:00 by jwe]
jwe
parents:
diff changeset
57 break;
05f6f120a65f [project @ 2007-08-31 17:58:00 by jwe]
jwe
parents:
diff changeset
58 endif
05f6f120a65f [project @ 2007-08-31 17:58:00 by jwe]
jwe
parents:
diff changeset
59 endif
05f6f120a65f [project @ 2007-08-31 17:58:00 by jwe]
jwe
parents:
diff changeset
60 h = get (h, "Parent");
05f6f120a65f [project @ 2007-08-31 17:58:00 by jwe]
jwe
parents:
diff changeset
61 endwhile
05f6f120a65f [project @ 2007-08-31 17:58:00 by jwe]
jwe
parents:
diff changeset
62 else
05f6f120a65f [project @ 2007-08-31 17:58:00 by jwe]
jwe
parents:
diff changeset
63 error ("ancestor: second argument must be a string or cell array of strings");
05f6f120a65f [project @ 2007-08-31 17:58:00 by jwe]
jwe
parents:
diff changeset
64 endif
05f6f120a65f [project @ 2007-08-31 17:58:00 by jwe]
jwe
parents:
diff changeset
65 else
05f6f120a65f [project @ 2007-08-31 17:58:00 by jwe]
jwe
parents:
diff changeset
66 print_usage ();
05f6f120a65f [project @ 2007-08-31 17:58:00 by jwe]
jwe
parents:
diff changeset
67 endif
05f6f120a65f [project @ 2007-08-31 17:58:00 by jwe]
jwe
parents:
diff changeset
68
05f6f120a65f [project @ 2007-08-31 17:58:00 by jwe]
jwe
parents:
diff changeset
69 endfunction