File: findobj.m

package info (click to toggle)
octplot 0.4.0-7
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 3,072 kB
  • ctags: 1,563
  • sloc: cpp: 8,133; ansic: 5,053; sh: 617; makefile: 161
file content (112 lines) | stat: -rw-r--r-- 3,494 bytes parent folder | download
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
## Copyright (C) 2007 Kai Habel
##
## This program is free software; you can redistribute it and/or modify it
## under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 2, or (at your option)
## any later version.
##
## OctPlot is distributed in the hope that it will be useful, but
## WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
## General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with OctPlot; see the file COPYING.  If not, write to the Free
## Software Foundation, 59 Temple Place - Suite 330, Boston, MA
## 02111-1307, USA.

## -*- texinfo -*-
## @deftypefn {Function File} {} @var{go} = findobj (@var{propname1}, @var{propvalue1}, ...)
## @deftypefnx {Function File} {} @var{go} = findobj (@var{h}, @var{propname1}, @var{propvalue1}, ...)
## @deftypefnx {Function File} {} @var{go} = findobj (@var{h}, 'flat', @var{propname1}, @var{propvalue1}, ...)
## @deftypefnx {Function File} {} @var{go} = findobj (@var{h})
## @deftypefnx {Function File} {} @var{go} = findobj ()
## Returns all graphic objects @var{go} that contain property/values pair(s) 
## @var{propname1}, @var{propvalue1}, ... .
## If @var{h} is given the search is restricted to object tree below @var{h} and @var{h} itself. 
## A second argument 'flat' restricts the search to the objects given in @var{h}.
## If no property/value pair(s) are given all graphic objects below @var{h} or the root are returned. 
##
## @seealso{isprop, ishandle, set, get}
## @end deftypefn

## Author: Kai Habel

function obj = findobj(varargin)
  obj = [];
  gobj = 0;
  arglist = varargin;
  flat = false;

  # find the 'special' cases
  if (length(arglist) > 0)
    if ishandle(arglist{1})
      gobj = arglist{1};
      arglist = arglist(2 : end);
      if (length(arglist) > 0)
        if strcmp(tolower(arglist(1)), 'flat')
          flat = true;
          arglist = arglist(2 : end);
        end
      end
    end
  end

  if (length(arglist) == 0)
    obj = [obj; gobj(:)];
    for i = 1 : length(gobj)
      if (isprop(gobj(i), 'Children'))
        ch = get(gobj(i), 'Children');
        if ((length(ch) > 0) & !flat)
          obj = [obj; findobj(ch)];
        end
      end
    end
  else
    # we have propname/propvalues pair(s)
    propnames = arglist(1 : 2 : end);
    propvalues = arglist(2 : 2 : end);
    if (flat)
      obj_lst = findobj(gobj, 'flat');
    else
      obj_lst = findobj(gobj);
    end
    for i = 1 : length(obj_lst)
      go = obj_lst(i);
      if (ishandle(go))
        incl = true;
        for i = 1 : length(propnames)
          # check each property 
          propname = propnames{i};
          propvalue = propvalues{i};
          h = [];
          if isprop(go, propname)
            val = get(go, propname);
            if (length(val) > 0)
              if isstr(val)
                # for string valued properties
                if (strcmp(val, tolower(propvalue)) == 1)
                  h = go;
                end
              else
                # for numeric properties
                if all(val == propvalue)
                  h = go;
                end
              end
            end
          end
          if (length(h) == 0)
            incl = false;
          end
        end

        if (incl)
          obj = [obj; h];
        end

      end
    end
  end
end