Issues with reading/using "*" and "#" in a string

2 visualizaciones (últimos 30 días)
Shawn
Shawn el 4 de Nov. de 2023
Editada: Dyuman Joshi el 6 de Nov. de 2023
I'm building a simple tone dialer for a lab, and I thought it would be cool to evaluate the user's input string into an array, and then evaluate each piece of that array so I can assign it to a sound that plays back to the user. Right now I've successfully been able to read and spit out the string with some accuracy, but when I join "*" or "#" with another number it seems to be messing with the ASCII value because it exits right out of my IF statement. I'll post my code as I have it right now, thanks in advance for any help.
P.S. This is very incomplete, just trying to make sure I can reasonably validate an input
-------------------------------------------------------------------------------------------------------
token1 = 0;
while token1 == 0
input2 = input('Please enter a 7 digit phone number: ','s');
[number, token1] = getNumber(input2);
end
----------------------------------------------------------------
function [out2,token] = getNumber(y)
inputNum = num2str(y) - '0';
for i = 1:length(inputNum)
if inputNum(i) >= 0 & inputNum <= 9 | inputNum == -6 | inputNum == -13
switch inputNum(i)
case 0
disp(inputNum(i))
case 1
disp(inputNum(i))
case 2
disp(inputNum(i))
case 3
disp(inputNum(i))
case 4
disp(inputNum(i))
case 5
disp(inputNum(i))
case 6
disp(inputNum(i))
case 7
disp(inputNum(i))
case 8
disp(inputNum(i))
case 9
disp(inputNum(i))
case -6
fprintf("*%c",newline);
case -13
fprintf("#%c",newline);
end
else
fprintf("That is not a valid phone number, please enter a valid number.%c",newline);
token = 0;
break
end
token = 1;
end
out2 = 0;
end

Respuesta aceptada

Dyuman Joshi
Dyuman Joshi el 4 de Nov. de 2023
Editada: Dyuman Joshi el 6 de Nov. de 2023
> You only used i-th term for the first comparison only, rest of the comparisons are with vector.
if inputNum(i) >= 0 & inputNum <= 9 | inputNum == -6 | inputNum == -13
Correct it -
if inputNum(i) >= 0 & inputNum(i) <= 9 | inputNum(i) == -6 | inputNum(i) == -13
And, as you are comparing with discrete values, you can use ismember -
if ismember(inputNum(i), [-6 -13 0:9])
Additionally, you can combine multiple cases which have the same statments -
switch inputNum(i)
case {0:9}
disp(inputNum(i))
case -6
fprintf("*%c",newline);
case -13
fprintf("#%c",newline);
end
Also, you might want to check the length on the input as well, if it is 7 digits or not.
  6 comentarios
Dyuman Joshi
Dyuman Joshi el 4 de Nov. de 2023
Yes, that is correct.
Though I will re-iterate one thing - you might want to include a check for the length of the input.
Because, right now, your code runs for an input with other number of digits.
Voss
Voss el 6 de Nov. de 2023
@Shawn: The values checked in your original case statements were correct:
double('0123456789*#' - '0')
ans = 1×12
0 1 2 3 4 5 6 7 8 9 -6 -13
Also, there is no need to use the newline function with fprintf, since the format spec argument to fprintf can contain '\n' to indicate newlines:
fprintf('*\n')
*
fprintf('#\n')
#
Or you could've used disp, as in the other cases:
disp('*')
*
disp('#')
#
And @Dyuman Joshi is correct that you can use a cell array to check for more than one value in a single case statement:
x = 5;
switch x
case {0:9} % scalar cell array containing the vector 0:9 [doesn't match x=5 because ~isequal(5,0:9)]
disp('match')
otherwise
disp('no match')
end
no match
But in this case it should be a cell array of scalars:
x = 5;
switch x
case num2cell(0:9) % 1x10 cell array containing scalars 0,1,...,9
disp('match')
otherwise
disp('no match')
end
match

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Data Type Conversion en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by