MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

# Creating the sounds of a touchtone keypad

Asked by Mark Grano on 26 Oct 2012

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!

## Products

No products are associated with this question.

Answer by Star Strider on 26 Oct 2012
Edited by 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.

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

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'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.

Answer by Walter Roberson on 26 Oct 2012

Are you looking for length(stra) ?

Mark Grano on 26 Oct 2012

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"

Mark Grano on 26 Oct 2012

is it possible to convert the string into an array?

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.