function hb_values_write ( output_unit, valcrd, mxtype, nnzero, neltvl, ...
  valfmt, values )

%*****************************************************************************80
%
%% HB_VALUES_WRITE writes the values of an HB matrix.
%
%  Discussion:
%
%    If the user is creating an HB file, then the user should already
%    have opened the file, and written the header and structure records.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    11 November 2003
%
%  Author:
%
%    John Burkardt
%
%  Reference:
%
%    Iain Duff, Roger Grimes, John Lewis,
%    User's Guide for the Harwell-Boeing Sparse Matrix Collection,
%    October 1992.
%
%  Input:
%
%    integer OUTPUT_UNIT, the unit to which data is written.
%
%    integer VALCRD, the number of input lines for numerical values.
%
%    character ( len = 3 ) MXTYPE, the matrix type.
%    First character is R for Real, C for complex, P for pattern only.
%    Second character is S for symmetric, U for unsymmetric, H for
%      Hermitian, Z for skew symmetric, R for rectangular.
%    Third character is A for assembled and E for unassembled
%      finite element matrices.
%
%    integer NNZERO.  In the case of assembled sparse matrices,
%    this is the number of nonzeroes.  In the case of unassembled finite
%    element matrices, in which the right hand side vectors are also
%    stored as unassembled finite element vectors, this is the total
%    number of entries in a single unassembled right hand side vector.
%
%    integer NELTVL, the number of finite element matrix entries,
%    set to 0 in the case of assembled matrices.
%
%    character ( len = 20 ) VALFMT, the format for reading values.
%
%    real VALUES(NNZERO) or VALUES(NELTVL), the nonzero values
%    of the matrix.
%
  if ( 0 < valcrd )
%
%  From the FORTRAN format string VALFMT, determine the number of
%  values per line, and their width.
%
    line = valfmt(2:length(valfmt));
    [ val_nperline, count, errmsg, nextindex ] = sscanf ( line, '%d' );
    line = line(nextindex+1:length(line));
    [ val_len, count, errmsg, nextindex ] = sscanf ( line, '%d' );
    s = int2str ( val_len );
    val_format = [ '%' s 'f' ];

    if ( mxtype(3:3) == 'A' )

      for j = 1 : nnzero
        fprintf ( output_unit, val_format, values(j) );
        if ( mod ( j, val_nperline ) == 0 ) 
          fprintf ( output_unit, '\n' );
        end
      end

      if ( mod ( nnzero, val_nperline ) ~= 0 )
        fprintf ( output_unit, '\n' );
      end

    elseif ( mxtype(3:3) == 'E' )

      for j = 1 : neltvl
        fprintf ( output_unit, val_format, values(j) );
        if ( mod ( j, val_nperline ) == 0 ) 
          fprintf ( output_unit, '\n' );
        end
      end

      if ( mod ( neltvl, val_nperline ) ~= 0 )
        fprintf ( output_unit, '\n' );
      end

    else

      fprintf ( 1, '\n' );
      fprintf ( 1, 'HB_VALUES_WRITE - Fatal error!\n' );
      fprintf ( 1, '  Illegal value of MXTYPE(3:3).\n' );
      stop

    end

  end

  return
end
