Creating the sounds of a touchtone keypad
Show older comments
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
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
12 Comments
Mark Grano
on 26 Oct 2012
Star Strider
on 26 Oct 2012
Edited: Star Strider
on 26 Oct 2012
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.
Mark Grano
on 26 Oct 2012
Star Strider
on 26 Oct 2012
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.
Mark Grano
on 26 Oct 2012
Star Strider
on 26 Oct 2012
Edited: Star Strider
on 26 Oct 2012
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.
Mark Grano
on 27 Oct 2012
Star Strider
on 27 Oct 2012
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.
Image Analyst
on 27 Oct 2012
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.
Star Strider
on 27 Oct 2012
@IA — That definitely did not make my day! I put in a fair amount of work on that answer.
Matt Fig
on 27 Oct 2012
I feel your pain, SS. You just gotta love duplicate questions and OP's who don't let you know...
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.
Walter Roberson
on 26 Oct 2012
0 votes
Are you looking for length(stra) ?
3 Comments
Mark Grano
on 26 Oct 2012
Mark Grano
on 26 Oct 2012
Walter Roberson
on 26 Oct 2012
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.
Categories
Find more on Creating, Deleting, and Querying Graphics Objects 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!