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 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
|
% SUBSREF [overload base function]
% Copyright Notice
%
% Copyright (C) 2015 CentraleSupelec
% Copyright (C) 2013 SUPELEC
%
% Author: Julien Bect <julien.bect@supelec.fr>
% Copying Permission Statement
%
% This file is part of
%
% STK: a Small (Matlab/Octave) Toolbox for Kriging
% (http://sourceforge.net/projects/kriging)
%
% STK 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 of the License, or (at your
% option) any later version.
%
% STK 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 STK. If not, see <http://www.gnu.org/licenses/>.
function t = subsref (x, idx)
switch idx(1).type
case '()'
L = length (idx(1).subs);
if L == 1, % linear indexing
t = subsref (x.data, idx);
elseif L == 2, % matrix-style indexing
I = idx(1).subs{1};
J = idx(1).subs{2};
x.data = x.data(I, J);
if ~ isempty (x.colnames),
x.colnames = x.colnames(1, J);
end
if ~ isempty (x.rownames),
x.rownames = x.rownames(I, 1);
end
t = x;
else
stk_error ('Illegal indexing.', 'IllegalIndexing');
end
case '{}'
errmsg = 'Indexing with curly braces is not allowed.';
stk_error (errmsg, 'IllegalIndexing');
case '.'
t = get (x, idx(1).subs);
end
if (length (idx)) > 1,
t = subsref (t, idx(2:end));
end
end % function @stk_dataframe.subsref
%!shared x, s, t, data
%! x = stk_dataframe(rand(3, 2));
%! s = {'a'; 'b'; 'c'};
%! t = {'xx' 'yy'};
%!test
%! x = set(x, 'rownames', s);
%! assert (isequal (x.rownames, s))
%! assert (isequal (x.rownames{2}, 'b'))
%!test
%! x = set(x, 'colnames', t);
%! assert (isequal (x.rownames, s))
%! assert (isequal (x.colnames, t))
%! assert (isequal (x.colnames{2}, 'yy'))
%--- tests with a bivariate dataframe + column names --------------------------
%!shared u, data
%! u = rand(3, 2);
%! data = stk_dataframe(u, {'x1', 'x2'});
%!assert (isequal (data.x2, u(:, 2)))
%!assert (data.x2(3) == u(3, 2))
%!error t = data.toto;
%!error t = data(1, 1).zzz; % illegal multilevel indexing
%!error t = data(1, 1, 1); % too many indices
%!error t = data{1}; % curly braces not allowed
%!test % legacy feature: data.a returns the 'mean' column if it exists
%! data = set(data, 'colnames', {'mean', 'x2'});
%! assert(isequal(data.a, u(:, 1)));
%!test % legacy feature: data.a returns the whole dataframe otherwise
%! data = set(data, 'colnames', {'x1', 'x2'});
%! assert(isequal(data.a, u));
%!test % select rows and columns
%! x = stk_dataframe (reshape (1:15, 5, 3), {'u' 'v' 'w'});
%! assert (isequal (x([3 5], 2), stk_dataframe ([8; 10], {'v'})));
%--- tests with a univariate dataframe ----------------------------------------
%!shared u, data
%! u = rand(3, 1); data = stk_dataframe(u, {'x'});
%!assert (isequal (data.x, u))
%!assert (isequal (double (data), u))
%!assert (isequal (double (data(2)), u(2)))
%!assert (isequal (double (data(3, 1)), u(3)))
%!error t = data(1, 1, 1); % too many indices
|