annotate yesnoQuestion.m @ 10:9551f4e7cb66

Added utility function for asking the user a yes or no question.
author Jonatan Werpers <jonatan@werpers.com>
date Mon, 21 Sep 2015 17:58:04 +0200
parents
children 0c166d195f58
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
10
9551f4e7cb66 Added utility function for asking the user a yes or no question.
Jonatan Werpers <jonatan@werpers.com>
parents:
diff changeset
1 function b = yesnoQuestion(question, defaultAnswer)
9551f4e7cb66 Added utility function for asking the user a yes or no question.
Jonatan Werpers <jonatan@werpers.com>
parents:
diff changeset
2 default_arg('defaultAnswer','y');
9551f4e7cb66 Added utility function for asking the user a yes or no question.
Jonatan Werpers <jonatan@werpers.com>
parents:
diff changeset
3
9551f4e7cb66 Added utility function for asking the user a yes or no question.
Jonatan Werpers <jonatan@werpers.com>
parents:
diff changeset
4 yesAnswer = {'y','Y','yes','Yes','YES'};
9551f4e7cb66 Added utility function for asking the user a yes or no question.
Jonatan Werpers <jonatan@werpers.com>
parents:
diff changeset
5 noAnswer = {'n','N','no','No','NO'};
9551f4e7cb66 Added utility function for asking the user a yes or no question.
Jonatan Werpers <jonatan@werpers.com>
parents:
diff changeset
6
9551f4e7cb66 Added utility function for asking the user a yes or no question.
Jonatan Werpers <jonatan@werpers.com>
parents:
diff changeset
7 switch defaultAnswer
9551f4e7cb66 Added utility function for asking the user a yes or no question.
Jonatan Werpers <jonatan@werpers.com>
parents:
diff changeset
8 case yesAnswer
9551f4e7cb66 Added utility function for asking the user a yes or no question.
Jonatan Werpers <jonatan@werpers.com>
parents:
diff changeset
9 optionString = '[Y/n]';
9551f4e7cb66 Added utility function for asking the user a yes or no question.
Jonatan Werpers <jonatan@werpers.com>
parents:
diff changeset
10 yesAnswer{end+1} = '';
9551f4e7cb66 Added utility function for asking the user a yes or no question.
Jonatan Werpers <jonatan@werpers.com>
parents:
diff changeset
11 case noAnswer
9551f4e7cb66 Added utility function for asking the user a yes or no question.
Jonatan Werpers <jonatan@werpers.com>
parents:
diff changeset
12 optionString = '[y/N]';
9551f4e7cb66 Added utility function for asking the user a yes or no question.
Jonatan Werpers <jonatan@werpers.com>
parents:
diff changeset
13 noAnswer{end+1} = '';
9551f4e7cb66 Added utility function for asking the user a yes or no question.
Jonatan Werpers <jonatan@werpers.com>
parents:
diff changeset
14 otherwise
9551f4e7cb66 Added utility function for asking the user a yes or no question.
Jonatan Werpers <jonatan@werpers.com>
parents:
diff changeset
15 error('Unrecognized default answer: %s', defaultAnswer);
9551f4e7cb66 Added utility function for asking the user a yes or no question.
Jonatan Werpers <jonatan@werpers.com>
parents:
diff changeset
16 end
9551f4e7cb66 Added utility function for asking the user a yes or no question.
Jonatan Werpers <jonatan@werpers.com>
parents:
diff changeset
17
9551f4e7cb66 Added utility function for asking the user a yes or no question.
Jonatan Werpers <jonatan@werpers.com>
parents:
diff changeset
18 b = [];
9551f4e7cb66 Added utility function for asking the user a yes or no question.
Jonatan Werpers <jonatan@werpers.com>
parents:
diff changeset
19 while isempty(b)
9551f4e7cb66 Added utility function for asking the user a yes or no question.
Jonatan Werpers <jonatan@werpers.com>
parents:
diff changeset
20 answer = input([question ' ' optionString ':'],'s');
9551f4e7cb66 Added utility function for asking the user a yes or no question.
Jonatan Werpers <jonatan@werpers.com>
parents:
diff changeset
21 switch answer
9551f4e7cb66 Added utility function for asking the user a yes or no question.
Jonatan Werpers <jonatan@werpers.com>
parents:
diff changeset
22 case yesAnswer
9551f4e7cb66 Added utility function for asking the user a yes or no question.
Jonatan Werpers <jonatan@werpers.com>
parents:
diff changeset
23 b = true;
9551f4e7cb66 Added utility function for asking the user a yes or no question.
Jonatan Werpers <jonatan@werpers.com>
parents:
diff changeset
24 case noAnswer
9551f4e7cb66 Added utility function for asking the user a yes or no question.
Jonatan Werpers <jonatan@werpers.com>
parents:
diff changeset
25 b = false;
9551f4e7cb66 Added utility function for asking the user a yes or no question.
Jonatan Werpers <jonatan@werpers.com>
parents:
diff changeset
26 end
9551f4e7cb66 Added utility function for asking the user a yes or no question.
Jonatan Werpers <jonatan@werpers.com>
parents:
diff changeset
27 end
9551f4e7cb66 Added utility function for asking the user a yes or no question.
Jonatan Werpers <jonatan@werpers.com>
parents:
diff changeset
28 end