Skip to content

Commit 98057ff

Browse files
committed
* inst/dbtable.m: provide default for head and tail and check input is
numeric
1 parent d178e52 commit 98057ff

File tree

1 file changed

+23
-2
lines changed

1 file changed

+23
-2
lines changed

inst/dbtable.m

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -507,7 +507,10 @@ function writetable (this, filename)
507507
endfunction
508508

509509
# head tail - create sub tables
510-
function tdata = head(this, rows)
510+
function tdata = head(this, rows=8)
511+
if nargin > 1 && !isnumeric(rows)
512+
error ("Expected rows to be a number");
513+
endif
511514
nrows = size(this, 1);
512515
if rows > nrows
513516
rows = nrows;
@@ -517,7 +520,10 @@ function writetable (this, filename)
517520
tdata = dbtable(data{:}, 'VariableNames', names);
518521
endfunction
519522

520-
function tdata = tail(this, rows)
523+
function tdata = tail(this, rows=8)
524+
if nargin > 1 && !isnumeric(rows)
525+
error ("Expected rows to be a number");
526+
endif
521527
nrows = size(this, 1);
522528
if rows >= nrows
523529
rows = 1;
@@ -619,3 +625,18 @@ function writetable (this, filename)
619625
%! s = table2struct(t);
620626
%! assert(fieldnames(s), {'V1'; 'V2'});
621627
%! assert(s.V1, [0;1;3]);
628+
629+
%!test
630+
%! V1 = [0;1;3];
631+
%! V2 = [2;4;6];
632+
%! t = dbtable(V1, V2);
633+
%! x = head(t);
634+
%! assert(length(x), 3);
635+
%! x = head(t, 1);
636+
%! assert(length(x), 1);
637+
%! assert(x.V1(1), 0);
638+
%! x = tail(t);
639+
%! assert(length(x), 3);
640+
%! x = tail(t, 1);
641+
%! assert(length(x), 1);
642+
%! assert(x.V1(1), 3);

0 commit comments

Comments
 (0)