]> Creatis software - CreaPhase.git/blob - octave_packages/dataframe-0.9.1/@dataframe/isfield.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / dataframe-0.9.1 / @dataframe / isfield.m
1 function [resu, idx] = isfield(df, name, strict)
2   
3   %# -*- texinfo -*-
4   %# @deftypefn {Function File} [@var{resu}, @var{idx}] = isfield
5   %# (@var{df}, @var{name}, @var{strict})
6   %# Return true if the expression @var{df} is a dataframe and it
7   %# includes an element matching @var{name}.  If @var{name} is a cell
8   %# array, a logical array of equal dimension is returned. @var{idx}
9   %# contains the column indexes of number of fields matching
10   %# @var{name}. To enforce strict matching instead of regexp matching,
11   %# set the third argument to 'true'.
12   %# @end deftypefn 
13
14   %% Copyright (C) 2009-2012 Pascal Dupuis <Pascal.Dupuis@uclouvain.be>
15   %%
16   %% This file is part of Octave.
17   %%
18   %% Octave is free software; you can redistribute it and/or
19   %% modify it under the terms of the GNU General Public
20   %% License as published by the Free Software Foundation;
21   %% either version 2, or (at your option) any later version.
22   %%
23   %% Octave is distributed in the hope that it will be useful,
24   %% but WITHOUT ANY WARRANTY; without even the implied
25   %% warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
26   %% PURPOSE.  See the GNU General Public License for more
27   %% details.
28   %%
29   %% You should have received a copy of the GNU General Public
30   %% License along with Octave; see the file COPYING.  If not,
31   %% write to the Free Software Foundation, 51 Franklin Street -
32   %% Fifth Floor, Boston, MA 02110-1301, USA.
33
34   %#
35   %# $Id: isfield.m 9585 2012-02-05 15:32:46Z cdemills $
36   %#
37
38   if !isa(df, 'dataframe'),
39     resu = false; return;
40   endif
41
42   if nargin <2 || nargin > 3,
43     print_usage();
44     resu = false; return;
45   endif
46
47   if 2 == nargin, strict = false; endif
48
49   if isa(name, 'char'),
50     if strict, %# use strmatch to get indexes
51       for indi = size(name, 1):-1:1,
52         dummy = strmatch(name(indi, :), df._name{2}, "exact");
53         resu(indi, 1) = !isempty(dummy);
54         for indj = 1:length(dummy),
55           idx(indi, indj) = dummy(indj);
56         endfor
57       endfor
58     else
59       for indi = size(name, 1):-1:1,
60         try
61           dummy = df_name2idx(df._name{2}, name(indi, :), \
62                               df._cnt(2), 'column');
63           resu(indi, 1) = !isempty(dummy);
64           for indj = 1:length(dummy),
65             idx(indi, indj) = dummy(indj);
66           endfor
67         catch
68           resu(indi, 1) = false; idx(indi, 1) = 0;
69         end_try_catch
70       endfor
71     endif
72   elseif isa(name, 'cell'),
73     if strict, %# use strmatch to get indexes
74       for indi = size(name, 1):-1:1,
75         dummy = strmatch(name{indi}, df._name{2}, "exact");
76         resu{indi, 1} = !isempty(dummy);
77         idx{indi, 1} = dummy;
78       endfor
79     else
80       for indi = length(name):-1:1,
81         try
82           dummy = df_name2idx(df._name{2}, name{indi}, \
83                               df._cnt(2), 'column');
84           keyboard
85           resu{indi, 1} = !isempty(dummy); idx{indi, 1} = dummy;
86         catch
87           resu{indi, 1} = false; cnt{indi, 1} = 0;
88         end_try_catch
89       endfor
90     endif
91   endif
92 endfunction