Trying to use user input directly into another switch case.

8 views (last 30 days)
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

Walter Roberson
Walter Roberson on 8 Nov 2022
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');
In the case that the random number was 1, then q2 and q3 are not assigned to. Likewise, for 2, q1 and q3 are not assigned to. For 3, q1 and q2 are not assigned to.
switch q1:q2:q3
If q1, q2, and q3 all existed somehow, then with those all being character vectors, the code be interpreted the same as
switch q1(1):q2(1):q3(1)
which would be like 'g':'b':'g' or like 'b':'b':'g' or whatever just happened to be in the variables. 'g' > 'b' but 'b'+'b' > 'g', so with the right combination of initial value and increments you that could evaluate to scalar 'b' or 'g' or to the empty character vector '' . And that is what would be compared to the cases. "good" does not match 'g' or 'b' and so on, so the "good" and "bad" cases cannot match on that switch statement as written.
Now... if you were to assign to the same variable in all three cases, you could switch on that one variable.
  3 Comments
Walter Roberson
Walter Roberson on 8 Nov 2022
MATLAB uses the : for several different meanings.
When you have an array such as
A = magic(5)
A = 5×5
17 24 1 8 15 23 5 7 14 16 4 6 13 20 22 10 12 19 21 3 11 18 25 2 9
then using a single : as a index means "all indices for that dimension", the same as index 1:end
A(2,:)
ans = 1×5
23 5 7 14 16
A(2,1:end)
ans = 1×5
23 5 7 14 16
A(2,1:size(A,2))
ans = 1×5
23 5 7 14 16
When you have an expression a:c then that is the same as a:1:c where a:b:c means:
  • if b > 0 then generate a vector that starts with a(1), and increments by b(1) each time, stopping at the last value that does not exceed c(1). So 3:5 is 3:1:5 which is [3, 3+1, 3+1+1] and then stop because 3+1+1+1 would exceed 5.
  • if b < 0 then you decrement from a starting point instead of increment, and stop when the final result would be less than the ending point; for exampe 5:-1:3 would be [5, 5-1, 5-1-1] and then stop becaus 5-1-1-1 would be less than 3
This is valid for character variables. 'bad':'good' would be 'b' : 1 : 'g' which would be ['b', 'c', 'd', 'e', 'f', 'g'] -- all entries in the start and end point values are ignored except for the first entry so 'bad':'good' is the same as 'brigadoon':'gollie gee willikers!'
... And that is really it. The : character has no special meaning with respect to switch / case constructs.
So, when you used
switch q1:q2:q3
MATLAB treated it as a case of the a:b:c situation, that q1 was to be the start point of the vector, that the increment was to be in q2, and the not-to-exceed value was in q3. But that is a problem in your code because only one of q1, q2, or q3 is defined.
There is no operation in MATLAB that says anything like, "if q1 exists, use its value; otherwise if q2 exists, use its value; otherwise use the value of q3". You could program that yourself,
if exist('q1', 'var')
q = q1;
elseif exist('q2', 'var')
q = q2;
else
q = q3;
end
switch q
but really it is a lot easier and less error-prone just to write
switch Rand
%uses the random integer to choose a random question
case 1
q = input('How is your day today? good? bad?\n', 's');
case 2
q = input('How are you feeling today? good? bad?\n', 's');
case 3
q = 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 q
using the same variable each time.
Kacper
Kacper on 8 Nov 2022
wow ok. You've completely managed to explain what I was struggling with very quickly. I see now that I was misusing the function of : completely which in turn was performing a completely different operation to what I needed. This makes much more sense now. As to using the same variable each time, this also now makes much more sense to me now that I see it written out this way. This is exactly what I was looking for. Thank you for taking the time to explain it in layman terms to me. I very much appreciate it.

Sign in to comment.

More Answers (1)

VBBV
VBBV on 8 Nov 2022
Edited: VBBV on 8 Nov 2022
switch q1:q2:q3
  5 Comments
VBBV
VBBV on 8 Nov 2022
Edited: VBBV 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
Kacper on 8 Nov 2022
Ah ok I see what you mean now. This makes much more sense to what I did before. I appreciate the help very much.

Sign in to comment.

Categories

Find more on Data Type Identification in Help Center and File Exchange

Tags

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!