Home > atmlab > handy > input_scalar.m

input_scalar

PURPOSE ^

INPUT_SCALAR Prompts for user input of scalar type.

SYNOPSIS ^

function v = input_scalar( s, vlow, vhigh, vdefault )

DESCRIPTION ^

 INPUT_SCALAR   Prompts for user input of scalar type.

    Works as INPUT but accepts only scalar input. It is also possible
    to define a range for allowed input and default value. For example:

    v = input_scalar( 'Select shoe size', 30, 50 );

     results in

       Select shoe size {30 <= v <= 50}: 

     If a default value is given, this is used for empty input, which
     otherwise not is allowed.
     
 FORMAT   v = input_scalar( s [, vlow, vhigh, vdefault ] )

 OUT      v          User input.
 IN       s          String describing the selection
 OPT      vlow       A lower limit for the selection. Default is [],
                     which means no lower limit.
          vhigh      An upper limit for the selection.Default is [],
                     which means no upper lower limit.
          vdefault   Default value.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

DOWNLOAD ^

input_scalar.m

SOURCE CODE ^

0001 % INPUT_SCALAR   Prompts for user input of scalar type.
0002 %
0003 %    Works as INPUT but accepts only scalar input. It is also possible
0004 %    to define a range for allowed input and default value. For example:
0005 %
0006 %    v = input_scalar( 'Select shoe size', 30, 50 );
0007 %
0008 %     results in
0009 %
0010 %       Select shoe size {30 <= v <= 50}:
0011 %
0012 %     If a default value is given, this is used for empty input, which
0013 %     otherwise not is allowed.
0014 %
0015 % FORMAT   v = input_scalar( s [, vlow, vhigh, vdefault ] )
0016 %
0017 % OUT      v          User input.
0018 % IN       s          String describing the selection
0019 % OPT      vlow       A lower limit for the selection. Default is [],
0020 %                     which means no lower limit.
0021 %          vhigh      An upper limit for the selection.Default is [],
0022 %                     which means no upper lower limit.
0023 %          vdefault   Default value.
0024 
0025 % HISTORY: 2003-03-07  Created by Patrick Eriksson
0026 
0027 function v = input_scalar( s, vlow, vhigh, vdefault )
0028 
0029 
0030 %=== Default values
0031 %
0032 if nargin < 2
0033   vlow = [];
0034 end
0035 if nargin < 3
0036   vhigh = [];
0037 end
0038 if nargin == 4
0039   default = 1;
0040   if ~isempty( vlow )  &  vdefault < vlow
0041     error('Inconsistency between default value and lower limit.');
0042   end
0043   if ~isempty( vhigh )  &  vdefault > vhigh
0044     error('Inconsistency between default value and upper limit.');
0045   end
0046 else
0047   default = 0;
0048 end
0049 
0050 
0051 %=== Create question string
0052 %
0053 sq = s;
0054 %
0055 if default
0056   sq = [ sq, ' (default = ', num2str(vdefault),')' ];
0057 end
0058 %
0059 if ~isempty( vlow )  |  ~isempty( vhigh )
0060   sq = [ sq, ' {' ];
0061   if~isempty( vlow )
0062     sq = [ sq, num2str(vlow), ' <= ' ];
0063   end
0064   sq = [ sq, 'v' ];  
0065   if~isempty( vhigh )
0066     sq = [ sq, ' <= ', num2str(vhigh) ];
0067   end
0068   sq = [ sq, '}' ];
0069 end
0070 %
0071 sq = [ sq, ': ' ];
0072 
0073 
0074 
0075 while 1
0076 
0077   v = input( sq );
0078 
0079   if ( default & isempty(v) )
0080     v = vdefault;
0081     return
0082   elseif isscalar(v)  &  ( isempty(vlow) | v >= vlow )  &  ...
0083                                                 ( isempty(vhigh) | v <= vhigh )
0084     return;
0085   end 
0086 
0087   fprintf('Incorrect selection. Please try again:\n');
0088 end

Generated on Mon 15-Sep-2014 13:31:28 by m2html © 2005