function [choice,extra]= get_choice(prompt,responses)
%
% Version 2.3
%
% Written by Phillip M. Feldman 7 May, 2008
%
% 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.
% Version 2.2, 5 Feb, 2008
% The function was modified to allow for a second argument that returns
% any additional tokens that follow the initial input token. For backwards
% compatibility, this second argument is optional.
% Version 2.1, 12-11-2007
% The function was modified to test whether responses is a cell array of
% strings. If not, it is assumed to contain a single string, in which
% case the comparison against the input text must be performed
% differently to ensure that the entire string is compared against the
% user input.
% Version 2.0, 4-4-2006
% To allow a variable number of strings in each set of responses, the
% function was modified to expect the allowed responses to be specified
% via a cell array of strings rather than a matrix of strings. The
% function was also modified to strip trailing blanks from user input.
while (1)
text= deblank(input(prompt, 's'));
% Check for the presence of an extra token or tokens. We can do this
% by searching for a blank. Because we have removed trailing blanks
% above, any blank must be followed by at least one non-blank character.
i= find(text==' ', 1, 'first');
if (isempty(i))
if (nargout>=2), extra= []; end
else
if (nargout>=2), extra= text(i+1:end); end
text= text(1:i-1);
end
for i= 1 : length(responses)
if (iscellstr(responses{i}))
% responses{i} is a cell array of strings. test for a match to
% any of the strings:
if (any(strcmpi(text,responses{i}(:))))
choice= i;
return;
end
else
% responses{i} is not a cell array of strings. test for exact
% string match:
if (strcmpi(text,responses{i}))
choice= i;
return;
end
end
end
if (strcmpi(text,'quit') | strcmp(text,'exit'))
error('User-requested stop.');
end
fprintf ('Enter a valid input or type quit to stop the program.\n');
end