Creating the sounds of a touchtone keypad

Hello, im working on a project that produces the sound of a keypad when you enter a phone number.
i need help distinguishing between characters and digits and making sure the user entered a ten digit phone number. I know i need to use the function isempty() but i am not sure how to fully use the function. What i have for this part is the following:
stra= input("Enter a ten digit phone number");
cnt=0; k=1;
while (k<20);
str2num(stra(k));
if (isempty(stra(k)) = 0)
cnt= cnt+1;
i am fairly certain im using isempty() incorrectly. and im actually not even sure if im on the right track. but none the less any help would be appreciated greatly. Thanks!

Answers (2)

Star Strider
Star Strider on 26 Oct 2012
Edited: Star Strider on 26 Oct 2012
I'm not certain I understand where you are in your project, but you probably don't need isempty. I suggest for a start to consider these:
% Read phone numnber in as a string:
stra = input('Enter a ten digit phone number: ', 's')
% Find any letters:
ltrs = regexpi(stra, '[A-Z]\w*')
You'll need to fill in whatever logic you require here either to be sure your stra variable contains all numbers (the input function will throw an error if any letters are entered, so you have to add the 's' argument to input to allow for letters), or to convert the letters to their keypad numeric equivalents if that is what you want to do.
% Convert all-number string phone number to a numeric vector:
for k1 = 1:10
phnr(k1,:) = str2double(strphnr(k1));
end
To output the keypad tones once you have generated them, see sound and its related functions.

12 Comments

Thank you for your answer and well i have the keytones all worked out and working, i just could not figure out how to exclude the characters such as hyphens when someone enters in the phone number. as in 575-435-6573 would need to exclude hyphens. and i need to make sure the user entered in 10 digits. This is just a pre requisite that needs to be filled before the keytones are determined and produced. so im not such much finding letters as i am exluding all characters such as letters and hyphens out of the equation. so even if someone where to say mess up and enter 575-j789-1234h, it would exclude the letters and hyphens.
Hyphens are easy:
hyph = strfind(x, '-')
The ltrs variable won't pick them up. It may be possible to do that with regexpi but I couldn't find it in the documentation, and my experiments using what I know about regexpi to identify hyphens didn't work. The ltrs variable will have the indices of all the upper- and lower-case letters in the string.
ok, im a bit confused. will using the ltrs and hyph variables delete the characters from the string?
They won't delete the characters and hyphens as written, but this addition will:
modx = x;
modx([hyph ltrs]) = [];
The modx variable (created so it wouldn't destroy x) will contain only numbers but will still be a character string. To convert it to a numeric vector, you can use the phnr array I described earlier.
so the ltrs and hyph variables will find all the letters and hypens and then do what exactly?
They just create the indices for the variables. The second line replaces them with the empty matrix [], i.e. nothing — it deletes them.
Also, I found a small glitch in the ltrs variable. Replace it with:
ltrs = regexpi(x, '([A-Z]\w*?)' )
If you want to capture only the numbers and aren't at all interested in letters or hyphens, this is the best way to do it:
nbrs = regexpi(x, '\d')
nbrx = x(nbrs)
Then nbrx will be a string variable with only the numbers.
wow that worked great! Thank you so much. i just have one very very minor issue with another area in which i need to make sure the number is ten digits long after everything is taken out, and if it is not ten digits long, it needs to ask to enter stra again and re run the program.
i have clear;clc;
stra =input('Enter a 10 digit phone number: ','s');
num2str(stra());
nbrs = regexpi(stra, '\d');
nbrx = stra(nbrs);
num2str(nbrx());
cnt=length(nbrx);
if length(nbrx)<10
clear;clc;
disp('Invalid input'); strb=input('Please enter a TEN digit phone number: ','s');
stra=strb;
end
a=(nbrx(1));
although when i run this fragment i get an error. any thoughts on how to fix this? i imagine its simple. its just when i re enter stra it does not put it through the first part of the program that converts stra into what nbrx is. i get an error that nbrx is undefined.
One problem I see is that stra is already a string, so you don't have to convert it to one with a num2str call. So is nbrx. I also suggest you delete your clear and clc calls — you don't need them. You can simply overwrite existing variables with new values.
The clear and clc commands are probably what are generating your error, because nbrx no longer exists when you call it after the if block. (You cleared it in the if block.)
Not important, but it might be helpful if you stated in your input statements that the phone number should be numbers only and not include letters, spaces, parentheses, periods, or hyphens. Remember, you're reading stra in as a string, so input will accept all of them.
I'll keep this open for a while in case you have any further questions.
I think it was already marked as solved in his duplicate question: http://www.mathworks.com/matlabcentral/answers/52014-re-entering-a-user-inputted-variable, at least they look pretty much the same to me.
@IA — That definitely did not make my day! I put in a fair amount of work on that answer.
I feel your pain, SS. You just gotta love duplicate questions and OP's who don't let you know...
Star Strider
Star Strider on 27 Oct 2012
Edited: Star Strider on 27 Oct 2012
Thank you Matt. I kept it open for several hours waiting for a reply, and when I didn't get one, figured it would wait until morning.
I appreciate your sympathies!
I'm not willing to stoop to ‘Thank you for formally accepting my answer’, but when the OP says ‘wow that worked great! Thank you so much.’ and doesn't accept it, it's difficult not to feel a bit betrayed.

Sign in to comment.

Are you looking for length(stra) ?

3 Comments

i tried this but i am not putting the numbers into an array when the user inputs them. It is all one string. and when i used this, it just gave me the value "1" because well, that was the only value in the "array"
is it possible to convert the string into an array?
Notice that input() by default is numeric input, so unless the user specifically encloses their response in '' then you get back one number rather than a string of numbers. Star Strider showed the change to have input() read strings, by using the 's' option.

Sign in to comment.

Categories

Find more on Creating, Deleting, and Querying Graphics Objects in Help Center and File Exchange

Asked:

on 26 Oct 2012

Community Treasure Hunt

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

Start Hunting!