Be the first to rate this file! 0 Downloads (last 30 days) File Size: 2.39 KB File ID: #19879

get_choice v2.3

by Phillip M. Feldman

 

11 May 2008 (Updated 12 May 2008)

allow user to choose among alternatives by typing one of several short strings

| Watch this File

File Information
Description

% get_choice displays a prompt, reads a response (string) from standard
% input, trims trailing blanks, converts to lower case, and compares the
% result against strings in the "responses" cell array. If the response
% matches an entry in the cell array, the index of the first matching
% entry is returned. If the user types 'quit' or 'exit', the program is
% stopped. If the user input does not match any of these conditions, an
% error message is generated and the prompt is displayed again.
%
% Notes:
%
% 1. To implement a default response, the calling program must include a
% null string ('') as one of the entries in the responses cell array.
%
% 2. To implement multiple versions of any given response, the calling
% program should use nested cell arrays (see the example below).
%
% Example:
%
% choice= get_choice('Do you want to continue? ', ...
% {{'n','no'},{'y','yes'},'maybe'});
%
% The above call returns 1 if the user enters 'n' or 'no', 2 if the user
% enters 'y' or 'yes', and 3 if the user enters 'maybe'.

% Revision History

% Version 2.3, 7 May, 2008

% Modified code to perform case-insensitive string comparisons rather than
% converting input to lower case. The main reason for this change is that
% the 'extra' string may contain the name of a workspace variable, in which
% case upper/lower case must be preserved.

MATLAB release MATLAB 7.6 (R2008a)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Please login to add a comment or rating.
Tag Activity for this File
Tag Applied By Date/Time
data import Phillip M. Feldman 22 Oct 2008 10:00:32
data export Phillip M. Feldman 22 Oct 2008 10:00:32
choice Phillip M. Feldman 22 Oct 2008 10:00:32
selection Phillip M. Feldman 22 Oct 2008 10:00:32
interaction Phillip M. Feldman 22 Oct 2008 10:00:32
interactive Phillip M. Feldman 22 Oct 2008 10:00:32

Contact us at files@mathworks.com