MATLAB: Converting letters to numbers in an array and vice versa

arraycellschardoit4medoubleno attempt

I need to create a function lettertonumber which converts each of the ascii codes corresponding to the letters a through z and space to the numbers 1 through 27. Then, I need to create the inverse function numbertoletter which takes a number 1 to 27 and returns the corresponding character. (i.e., numbertoletter(20) = 't').

Best Answer

>> letter2number = @(c)1+lower(c)-'a';
>> letter2number('t')
ans =
20
>> letter2number('a')
ans =
1
>> number2letter = @(n)char(n-1+'a');
>> number2letter(20)
ans =
t
>> number2letter(1)
ans =
a
You will need to add a special case for the space character.
Related Question