Creating the sounds of a touchtone keypad

4 次查看(过去 30 天)
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!

回答(2 个)

Star Strider
Star Strider 2012-10-26
编辑:Star Strider 2012-10-26
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 个评论
Matt Fig
Matt Fig 2012-10-27
I feel your pain, SS. You just gotta love duplicate questions and OP's who don't let you know...
Star Strider
Star Strider 2012-10-27
编辑:Star Strider 2012-10-27
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
Walter Roberson 2012-10-26
Are you looking for length(stra) ?
  3 个评论
Mark Grano
Mark Grano 2012-10-26
is it possible to convert the string into an array?
Walter Roberson
Walter Roberson 2012-10-26
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 MATLAB 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by