Home > atmlab > handy > ascii_menu.m

ascii_menu

PURPOSE ^

ASCII_MENU User selection by an ASCII menu.

SYNOPSIS ^

function k = ascii_menu( xHeader, defNumber, varargin )

DESCRIPTION ^

 ASCII_MENU   User selection by an ASCII menu.

    Works as MENU, but always shows an ASCII menu and allows the definition
    of a default answer.

    The function ensures that the answer is valid. With a default option
    an empty input selects the default value.

    Command window example:
    >> K = ASCII_MENU('Choose a color',2,'Red','Blue','Green')
    displays on the screen:

      ----- Choose a color -----

         1) Red
         2) Blue (default)
         3) Green

         Select a menu number:


 FORMAT   k = ascii_menu( xHeader, defNumber, varargin )

 OUT      k
 IN       xHeader     Header text for menu.
          defNumber   Index for default value. [] means no default.
          varargin    Menu items.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

DOWNLOAD ^

ascii_menu.m

SOURCE CODE ^

0001 % ASCII_MENU   User selection by an ASCII menu.
0002 %
0003 %    Works as MENU, but always shows an ASCII menu and allows the definition
0004 %    of a default answer.
0005 %
0006 %    The function ensures that the answer is valid. With a default option
0007 %    an empty input selects the default value.
0008 %
0009 %    Command window example:
0010 %    >> K = ASCII_MENU('Choose a color',2,'Red','Blue','Green')
0011 %    displays on the screen:
0012 %
0013 %      ----- Choose a color -----
0014 %
0015 %         1) Red
0016 %         2) Blue (default)
0017 %         3) Green
0018 %
0019 %         Select a menu number:
0020 %
0021 %
0022 % FORMAT   k = ascii_menu( xHeader, defNumber, varargin )
0023 %
0024 % OUT      k
0025 % IN       xHeader     Header text for menu.
0026 %          defNumber   Index for default value. [] means no default.
0027 %          varargin    Menu items.
0028 
0029 % HISTORY: 2003-03-07  Created by Patrick Eriksson
0030 
0031 % The main part of the code is copied from an internal
0032 % sub-function of menu.m.
0033 
0034 
0035 function k = ascii_menu( xHeader, defNumber, varargin )
0036 
0037 
0038 if isempty( defNumber )
0039   default = 0;
0040 else
0041   default = 1;
0042 end
0043 
0044 
0045 
0046 %-------------------------------------------------------------------------
0047 % Calculate the number of items in the menu
0048 %-------------------------------------------------------------------------
0049 numItems = length(varargin);
0050 
0051 %-------------------------------------------------------------------------
0052 % Continuous loop to redisplay menu until the user makes a valid choice
0053 %-------------------------------------------------------------------------
0054 while 1,
0055     % Display the header
0056     disp(' ')
0057     disp(['----- ',xHeader,' -----'])
0058     disp(' ')
0059     % Display items in a numbered list
0060     for n = 1 : numItems
0061       if default  &  defNumber == n
0062         disp( [ '      ' int2str(n) ') ' varargin{n}, ' (default)'] )
0063       else
0064         disp( [ '      ' int2str(n) ') ' varargin{n} ] )
0065       end
0066     end
0067     disp(' ')
0068     % Prompt for user input
0069     k = input('Select a menu number: ');
0070     % Check input:
0071     % 1) make sure k has a value, or set to default
0072     if isempty(k)
0073       if default 
0074         k = defNumber;
0075       else
0076         k = -1; 
0077       end
0078     end;
0079     % 2) make sure the value of k is valid
0080     if  (k < 1) | (k > numItems) ...
0081         | ~strcmp(class(k),'double') | rem(k,1) ~= 0 ...
0082         | ~isreal(k) | (isnan(k)) | isinf(k),
0083         % Failed a key test. Ask question again
0084         disp(' ')
0085         disp('Invalid selection. Try again.')
0086     else
0087         % Passed all tests, exit loop and return k
0088         return
0089     end % if k...
0090 end % while 1

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