## Copyright (C) 2025
##
## 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 3 of the License, 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, see
## <https://www.gnu.org/licenses/>.

## -*- texinfo -*-
## @deftypefn {} {@var{h} =} height (@var{tbl})
## Return the number of rows in table @var{tbl}.
##
## @code{height(@var{tbl})} returns the number of rows in the table, which is
## equivalent to @code{size(@var{tbl}, 1)}.
##
## @example
## @group
## tbl = table([1; 2; 3], [4; 5; 6], 'VariableNames', @{'A', 'B'@});
## h = height(tbl)
## @result{} h = 3
## @end group
## @end example
##
## @seealso{width, size, table}
## @end deftypefn

function h = height (tbl)
  if (nargin != 1)
    print_usage ();
  endif

  ## REQ-TABLE-005: Accept structs as well as tables
  if (! istable (tbl) && ! isstruct (tbl))
    error ("height: input must be a table or struct");
  endif

  ## Ensure Properties exist
  if (istable (tbl))
    tbl = __ensure_properties__ (tbl);
  endif

  ## For tables (structs with Properties), get height from first data field
  if (isfield (tbl, "Properties") && isfield (tbl.Properties, "VariableNames"))
    var_names = tbl.Properties.VariableNames;
    if (! isempty (var_names) && isfield (tbl, var_names{1}))
      ## Get height from first variable
      first_var = tbl.(var_names{1});
      if (isvector (first_var))
        h = length (first_var);
      elseif (ismatrix (first_var))
        h = size (first_var, 1);
      else
        h = size (first_var, 1);
      endif
    else
      h = 0;
    endif
  else
    ## For regular structs, use size
    h = size (tbl, 1);
  endif
endfunction

