This tutorial will discuss determining which characters are letters using the
isletter() function in Matlab.
Check Which Characters Are Letters Using the
isletter() Function in MATLAB
A string or array of characters can contain letters, and numbers like the address of a house can contain letters as well as numbers. If we want to find if the given character, character array, or string contains a letter, we can use Matlab’s built-in function
In the case of single character input, the
isletter() function returns 1 or 0 depending on the character. If the character is a letter, the
isletter() function will return one or true, and if the character is not a letter, then the
isletter() function will return 0 or false.
For example, let’s check a given character if it’s a letter or not. See the code below.
c = '5'; isletter(c)
ans = logical 0
In the output, the result is 0, which means the given character is not a letter.
We can also check if a string or array of characters contains letters or not. If a character inside the string or array of characters is a letter, then the
isletter() function will return 1 in its place and 0 elsewhere.
For example, let’s create a string containing letters as well as numbers and check how many letters are present inside the string. See the code below.
c = '25 street'; isletter(c)
ans = 1×9 logical array 0 0 0 1 1 1 1 1 1
In the output, we can see that the first three characters are not letters because the output is 0 on the first three places, and the rest of the characters are letters because the rest of the output contains ones.
Space is also considered a character that why we can see output contains a 0 at the place of the space.
The input can be a scalar, vector, matrix, or multidimensional array. The output will be a logical array.
isnumeric() function can be used to check if the input is a number or not, and
isstring() function can be used to check if the input is a string or not.
We can also perform operations on the input string after finding if it contains a letter or not.
For example, consider we want to extract the letters present in a string. In this case, we can use the
isletter() function to check if the letters are present in the string or not.
If letters are present in the string, we will find their indices using the
find() function, and then we can extract them using these indices. See the code below.
OldString = '25 street' l = isletter(OldString); indices = find(l==1); NewString = OldString(indices)
OldString = '25 street' NewString = 'street'
In the output, the new string only contains letters.