[afaced]: / inst / @dataframe / isfield.m  Maximize  Restore  History

Download this file

93 lines (85 with data), 3.1 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
function [resu, idx] = isfield(df, name, strict)
%# -*- texinfo -*-
%# @deftypefn {Function File} [@var{resu}, @var{idx}] = isfield
%# (@var{df}, @var{name}, @var{strict})
%# Return true if the expression @var{df} is a dataframe and it
%# includes an element matching @var{name}. If @var{name} is a cell
%# array, a logical array of equal dimension is returned. @var{idx}
%# contains the column indexes of number of fields matching
%# @var{name}. To enforce strict matching instead of regexp matching,
%# set the third argument to 'true'.
%# @end deftypefn
%% Copyright (C) 2009-2012 Pascal Dupuis <Pascal.Dupuis@uclouvain.be>
%%
%% This file is part of Octave.
%%
%% Octave 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.
%%
%% Octave 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 Octave; see the file COPYING. If not,
%% write to the Free Software Foundation, 51 Franklin Street -
%% Fifth Floor, Boston, MA 02110-1301, USA.
%#
%# $Id$
%#
if (~isa (df, 'dataframe'))
resu = false; return;
endif
if (nargin < 2 || nargin > 3)
print_usage ();
resu = false; return;
endif
if (2 == nargin) strict = false; endif
if (isa (name, 'char'))
if (strict) %# use strmatch to get indexes
for indi = (size (name, 1):-1:1)
dummy = strmatch (name(indi, :), df._name{2}, "exact");
resu(indi, 1) = ~isempty (dummy);
for indj = (1:length (dummy))
idx(indi, indj) = dummy(indj);
endfor
endfor
else
for indi = (size (name, 1):-1:1)
try
dummy = df_name2idx (df._name{2}, name(indi, :), \
df._cnt(2), 'column');
resu(indi, 1) = ~isempty (dummy);
for indj = (1:length (dummy))
idx(indi, indj) = dummy(indj);
endfor
catch
resu(indi, 1) = false; idx(indi, 1) = 0;
end_try_catch
endfor
endif
elseif (isa (name, 'cell'))
if (strict) %# use strmatch to get indexes
for indi = (size (name, 1):-1:1)
dummy = strmatch (name{indi}, df._name{2}, "exact");
resu{indi, 1} = ~isempty (dummy);
idx{indi, 1} = dummy;
endfor
else
for indi = (length (name):-1:1)
try
dummy = df_name2idx (df._name{2}, name{indi}, \
df._cnt(2), 'column');
keyboard
resu{indi, 1} = ~isempty (dummy); idx{indi, 1} = dummy;
catch
resu{indi, 1} = false; cnt{indi, 1} = 0;
end_try_catch
endfor
endif
endif
endfunction