How to make the question reappear if someone mess up with the number ? eg. 0 to 4. And then someone type 5.
2 views (last 30 days)
Show older comments
MUHAMMAD LUQMAN CHE LAH
on 16 Jan 2022
Commented: Walter Roberson
on 16 Jan 2022
ques_1 = input('\nIn the last month, how often have you been upset because of something that happened unexpectedly ?\n');
if they answer 5 , how i want to get back to the first question.
0 Comments
Accepted Answer
Walter Roberson
on 16 Jan 2022
while true
ques_1 = input('\nIn the last month, how often have you been upset because of something that happened unexpectedly ?\n');
if isnumeric(ques_1) && isscalar(quest_1) && ismember(ques_1, 0:4)
break;
end
end
5 Comments
Walter Roberson
on 16 Jan 2022
isscalar(EXPRESSION is
isequal(size(EXPRESSION), [1 1])
That is, the expression must have exactly one row and one column (and no further dimensions) -- not a vector, not an array, and not empty. (Empty variables have length 0 in at least one dimension.)
isnumeric(EXPRESSION) is
ismember(class(EXPRESSION), {'single', 'double', 'int8', 'uint8', 'int16', 'uint16', 'int32', 'uint32', 'int64', 'uint64'})
In particular, isnumeric() does not include char or string or cell array or struct or logical -- just the built-in numeric datatypes
ismember(EXPRESSION, NUMERIC_LIST) is
OUTPUT = false(size(EXPRESSION));
for K = 1 : numel(EXPRESSION)
if any(EXPRESSION(K) == NUMERIC_LIST(:))
OUTPUT(K) = true;
end
end
So after we have ensured that ques_1 is scalar and numeric, then
ismember(ques_1, 0:4)
would mean the same as
ques_1 == 0 | ques_1 == 1 | ques_1 == 2 | ques_1 == 3 | ques_1 == 4
More Answers (0)
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!