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
|
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-2017 Pascal Dupuis <cdemills@gmail.com>
%%
%% This file is part of the dataframe package for Octave.
%%
%% This package 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 3, or (at your option) any later version.
%%
%% This package 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 this package; see the file COPYING. If not,
%% see <http://www.gnu.org/licenses/>.
if (~isa (df, 'dataframe'))
resu = false; return;
end
if (nargin < 2 || nargin > 3)
print_usage ();
resu = false; return;
end
if (2 == nargin) strict = false; end
if (isa (name, 'char'))
if (strict) %# use strmatch to get indexes
for indi = (size (name, 1):-1:1)
dummy = strmatch (name(indi, :), df.x_name{2}, 'exact');
resu(indi, 1) = ~isempty (dummy);
for indj = (1:length (dummy))
idx(indi, indj) = dummy(indj);
end
end
else
for indi = (size (name, 1):-1:1)
try
dummy = df_name2idx (df.x_name{2}, name(indi, :), ...
df.x_cnt(2), 'column');
resu(indi, 1) = ~isempty (dummy);
for indj = (1:length (dummy))
idx(indi, indj) = dummy(indj);
end
catch
resu(indi, 1) = false; idx(indi, 1) = 0;
end
end
end
elseif (isa (name, 'cell'))
if (strict) %# use strmatch to get indexes
for indi = (size (name, 1):-1:1)
dummy = strmatch (name{indi}, df.x_name{2}, 'exact');
resu{indi, 1} = ~isempty (dummy);
idx{indi, 1} = dummy;
end
else
for indi = (length (name):-1:1)
try
dummy = df_name2idx (df.x_name{2}, name{indi}, ...
df.x_cnt(2), 'column');
keyboard
resu{indi, 1} = ~isempty (dummy); idx{indi, 1} = dummy;
catch
resu{indi, 1} = false; cnt{indi, 1} = 0;
end
end
end
end
end
|