How to make the question reappear if someone mess up with the number ? eg. 0 to 4. And then someone type 5.

2 Ansichten (letzte 30 Tage)
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.

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 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 Kommentare
MUHAMMAD LUQMAN CHE LAH
MUHAMMAD LUQMAN CHE LAH am 16 Jan. 2022
Thank you Mr. Roberson, may i know what is the meaning of function isnumeric,isscalar and ismember ?
I have read the documentation and still do not understand it.
Perhaps you can explain it briefly.
Thank you in advance. :')
Walter Roberson
Walter Roberson am 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

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by