Trying to use user input directly into another switch case.
Show older comments
Hello,
I am receiving an error about an "Unrecognized function or variable 'q3' " even though the variables are storing the user input.
EDITOR:
%meant to generate random integer between 1-3
Rand = randi(3);
clc
switch Rand
%uses the random integer to choose a random question
case 1
q1 = input('How is your day today? good? bad?\n', 's');
case 2
q2 = input('How are you feeling today? good? bad?\n', 's');
case 3
q3 = input('What do you think of the course so far? good? bad?\n', 's');
end
%the user input, either "good" or "bad", is meant to return
% the appropriate sentence no matter which question was
%initially chosen.
switch q1:q2:q3
case "good"
fprintf('%i\n', 'I am very happy to hear that!')
case 'bad'
fprintf('%i\n', 'Oh dear. I hope it gets better soon!')
otherwise
fprintf('%i\n', 'I do not have an answer for this')
end
COMMAND WINDOW:
How is your day today? good? bad?
good
Unrecognized function or variable 'q3'.
Error in yurr (line 17)
switch q1:q2:q3
Not sure whether this is the best method for this type of thing however the main goal is to ask a question, receive an answer from the user and then give some form of reply based on that answer.
I apologise if this post is inappropriately formatted.
Accepted Answer
More Answers (1)
switch q1:q2:q3
5 Comments
VBBV
on 8 Nov 2022
Switch will attempt to use a condition to select specific case and execute statements for tht case.
VBBV
on 8 Nov 2022
Why not just put all fprintf statements inside each case ? Instead of separate switch case ?
Kacper
on 8 Nov 2022
%meant to generate random integer between 1-3
Rand = randi(3)
clc
switch Rand
%uses the random integer to choose a random question
case 1
% q1 = input('How is your day today? good? bad?\n', 's');
q1 = "good" % user input example
if strcmp(q1,"good")
fprintf('I am very happy to hear that!\n' )
elseif strcmp(q1,"bad")
fprintf( 'Oh dear. I hope it gets better soon!')
else
fprintf( 'I do not have an answer for this')
end
case 2
% q2 = input('How are you feeling today? good? bad?\n', 's');
q2 = "bad" % user input example
if strcmp(q2,"good")
fprintf( 'I am very happy to hear that!')
elseif strcmp(q2,"bad")
fprintf( 'Oh dear. I hope it gets better soon!')
else
fprintf( 'I do not have an answer for this')
end
case 3
% q3 = input('What do you think of the course so far? good? bad?\n', 's');
q3 = "" % user input example
if strcmp(q3,"good")
fprintf( 'I am very happy to hear that!')
elseif strcmp(q3,"bad")
fprintf( 'Oh dear. I hope it gets better soon!')
else
fprintf( 'I do not have an answer for this')
end
end
%the user input, either "good" or "bad", is meant to return
% the appropriate sentence no matter which question was
%initially chosen.
You can check if entered input using an if-else condition instead of seperate switch case
Kacper
on 8 Nov 2022
Categories
Find more on Data Type Identification 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!