function keyword_test ( )

%*****************************************************************************80
%
%% keyword_test() tests an example of Name/Value keyword input.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    09 February 2020
%
%  Author:
%
%    John Burkardt
%    based on a Rosetta Code example.
%
  timestamp ( );
  fprintf ( 1, '\n' );
  fprintf ( 1, 'keyword_test():\n' );
  fprintf ( 1, '  MATLAB/Octave version "%s"\n', version ( ) );
  fprintf ( 1, '  Demonstrate how to use Name/Value keyword input.\n' );

  fprintf ( 1, '\n' );
  fprintf ( 1, 'foo ( )\n' );
  foo ( );

  fprintf ( 1, '\n' );
  fprintf ( 1, 'foo ( ''param2'', ''b1'' )\n' );
  foo ( 'param2', 'b1' )

  fprintf ( 1, '\n' );
  fprintf ( 1, 'foo ( ''param1'', ''a2'', ''param2'', ''b2'' )\n' );
  foo ( 'param1', 'a2', 'param2', 'b2' ); 

  fprintf ( 1, '\n' );
  fprintf ( 1, 'foo ( ''param2'', ''b3'', ''param1'', ''a3'' )\n' );
  foo ( 'param2', 'b3', 'param1', 'a3' );  

  fprintf ( 1, '\n' );
  fprintf ( 1, 'foo ( ''param3'', ''c4'', ''param1'', ''a4'' )\n' );
  foo ( 'param3', 'c4', 'param1', 'a4' );

  fprintf ( 1, '\n' );
  fprintf ( 1, 'foo ( ''param1'', ''a5'', ''param2'', ''b5'', ''param3'', ''c5'' )\n' );
  foo ( 'param1', 'a5', 'param2', 'b5', 'param3', 'c5' );
%
%  Repeat.
%
  fprintf ( 1, '\n' );
  fprintf ( 1, 'foo ( ''param1'', ''a6'', ''param1'', ''a7'', ''param1'', ''a8'' )\n' );
  foo ( 'param1', 'a6', 'param1', 'a7', 'param1', 'a8' );
%
%  Unknown name.
%
  fprintf ( 1, '\n' );
  fprintf ( 1, 'foo ( ''bogus'', ''z9'' )\n' );
  foo ( 'bogus', 'z9' );
%
%  Not enough pairs.
%
  fprintf ( 1, '\n' );
  fprintf ( 1, 'foo ( ''param1'', ''a10'', ''param2'' )\n' );
  foo ( 'param1', 'a10', 'param2' );
%
%  Terminate.
%
  fprintf ( 1, '\n' );
  fprintf ( 1, 'keyword_test():\n' );
  fprintf ( 1, '  Normal end of execution.\n' );
  timestamp ( );

  return
end
function timestamp ( )

%*****************************************************************************80
%
%% timestamp() prints the current YMDHMS date as a timestamp.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    14 February 2003
%
%  Author:
%
%    John Burkardt
%
  t = now;
  c = datevec ( t );
  s = datestr ( c, 0 );
  fprintf ( 1, '%s\n', s );

  return
end
function foo ( varargin ) 

%*****************************************************************************80
%
%% foo() is an example of a function accepting Name/Value keyword input.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    09 February 2020
%
%  Author:
%
%    John Burkardt
%    based on a Rosetta Code example.
%
%  Input:
%
%    Name/Value pairs as follows, in any order:
%    'param1', 'value': the name 'param1', followed by a string value.
%    'param2', 'value': the name 'param2', followed by a string value.
%    'param3', 'value': the name 'param3', followed by a string value.
%

%
%  Default parameter values.
%
  param1 = '?';
  param2 = '?';
  param3 = '?';
%
%  Count the arguments.
%
  nargs = length ( varargin );
%
%  Read pairs of input arguments.
%
  for k = 1 : 2 : nargs
    if ( nargs < k + 1 )
      fprintf ( 1, 'foo - Even number of Name/value pairs required!\n' );
    else
      switch (varargin{k})
        case { 'param1' }
          param1 = varargin{k+1};
        case { 'param2' }
          param2 = varargin{k+1};
        case { 'param3' }
          param3 = varargin{k+1};
        otherwise
          fprintf ( 1, 'foo - Unrecognized parameter name "%s"!\n', varargin{k} );
      end
    end
  end 
%
%  Report final values.
%
  fprintf ( 1, 'param1 = : "%s"\n', param1 );	 
  fprintf ( 1, 'param2 = : "%s"\n', param2 );
  fprintf ( 1, 'param3 = : "%s"\n', param3 );	   
 
  return
end

